0

Ok, I need help with this one. I have defined a resource with non-standard actions. It looks like this in config/routes.rb:

1 Upload::Application.routes.draw do
2
3   resources :lib_imports, :only => [:index, :new, :create, :show] do
4     get 'reimport', :on => :member
5   end
  ...

I can see the routes I expect when I run rails routes

$ rake routes
reimport_lib_import GET    /lib_imports/:id/reimport(.:format) {:action=>"reimport", :controller=>"lib_imports"}
        lib_imports GET    /lib_imports(.:format)              {:action=>"index", :controller=>"lib_imports"}
     new_lib_import GET    /lib_imports/new(.:format)          {:action=>"new", :controller=>"lib_imports"}
    edit_lib_import GET    /lib_imports/:id/edit(.:format)     {:action=>"edit", :controller=>"lib_imports"}
         lib_import GET    /lib_imports/:id(.:format)          {:action=>"show", :controller=>"lib_imports"}
...

I'm trying to use the named route "reimport_lib_import" in the index view for the LibImport model, index.html.erb, part of which looks like this:

...
10 <% @lib_imports.each do |lib_import| %>
11   <tr>
12     <td><%= lib_import.spreadsheet %></td>
13     <td><%= link_to 'Show', lib_import %></td>
14     <td><%= link_to 'Re-import', reimport_lib_import %></td>
15   </tr>
16 <% end %>
...

However, the result is that the browser displays an error. The relevant part of the error is:

Showing /home/mike/rails_projects/experiments/upload/app/views/lib_imports/index.html.erb where line #14 raised:

undefined local variable or method `reimport_lib_import' for #<#<Class:0x007fa938a06778>:0x007fa938950720>

Extracted source (around line #14):

11:   <tr>
12:     <td><%= lib_import.spreadsheet %></td>
13:     <td><%= link_to 'Show', lib_import %></td>
14:     <td><%= link_to 'Re-import', reimport_lib_import %></td>
15:   </tr>
16: <% end %>
17: </table>

My the question is this: why does Rails complain about reimport_lib_import when I try to use it? The fact that it sails by lib_import on the previous line without complaint but trips over this one has me quite puzzled Anyone got any ideas?

Mike E
  • 5,493
  • 1
  • 14
  • 15
  • 2
    Does using `reimport_lib_import_url` instead of `reimport_lib_import` change anything? I'm a little rusty with Rails 2. – Devin M Nov 25 '11 at 04:46
  • reimport_lib_import_url or reimport_lib_import_path should work. Let me know if you get error again. – Bhushan Lodha Nov 25 '11 at 07:16
  • Devin and Bhushan, you are both right. Thanks for the comments. Devin I'm using Rails 3, but am curious: what was it in my question that made you think it was Rails 2? Am I doing something that is obsolete in Rails 3? – Mike E Nov 25 '11 at 15:15

1 Answers1

1

You should be using

<td><%= link_to 'Show', lib_import_path(lib_import) %></td>
<td><%= link_to 'Re-import', reimport_lib_import_path(lib_import) %></td>

The names generated (shown) in the rake routes need to be followed by _path for a relative path, or _url for a full url. Secondly, if it is a member path, you need to specify the member, in this case: lib_import itself.

Hope this helps.

nathanvda
  • 49,707
  • 13
  • 117
  • 139