Ive been trying to get this to work for 3 days now. I cant seem to understand why it doesn work. When the user clicks that link_to tag on the view it should execute the csv function in the controller. Instread I get an exception.
Here are the relevant files
Controller file: (users_controller.rb)
def csv
end
(Routes.rb):
resources :users
View: (show.html.erb)
<%= link_to 'Click HERE to open file', @user.image.url %><br/><br/><br/>
<%= label_tag(:q, "Parse CSV File:") %><br/>
<%= link_to 'CSV', csv_user_path %>
<% end %>
When I click on the "link_to 'CSV'.. tag above. It gives me this exception:
NameError in Users#show
Showing /Users/AM/Documents/RailsWS/bmc_mam/app/views/users/show.html.erb where line #47 raised:
Extracted source (around line #47):
44:
45: <%= label_tag(:q, "Parse CSV File:") %><br/>
46:
47: <%= link_to 'CSV', csv_user_path %>
48:
49:
50:
The browser URL is as follows when the exception occurs:
http://localhost:3000/users/28
Its clearly getting to the function but the URL mapping /routing is clearly messed up. I m wondering how to fix it.....Ive tried several approaches over the past few days, none seem to be working. This is the closest Ive gotten to making it work as in ...
Thanks in advance for your help.
After post answer posted below by @Gavin Miller:
I cahnged my routes.rb file to this:
get 'csv' => 'users#csv'
resources :users
Now Im getting this exception:
NameError in Users#show
Showing /Users/AM/Documents/RailsWS/bmc_mam/app/views/users/show.html.erb where line #47 raised:
undefined local variable or method `csv_user_path' for #<# <Class:0x00000104e7f6f8>:0x00000103a23c68>
Extracted source (around line #47):
44:
45: <%= label_tag(:q, "Parse CSV File:") %><br/>
46:
47: <%= link_to 'CSV', csv_user_path %>
48:
49:
50:
Just declaring resources :users
is only going to infer the 7 standard RESTful actions1. if you want csv
to be accepted as a route, you'll have to explicitly define it:
get 'csv' => 'controller#action'
where controller == users
and action == csv
.
Re-addressing the edited question... You'll need to pass a user object to the csv_user_path
function:
<%= link_to 'CSV', csv_user_path(@user) %>
It also appears like you'd like the url to be associated with the user, so you can declare it as a member
to get a url like: http://localhost:3000/users/28/csv
for that result you can use this code:
resources :users do
member do
get 'csv'
end
end
index, new, create, show, edit, update, destroy
as seen in Rails Routing Guide