好的,我需要帮助。我已经使用非标准操作定义了资源。在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
...
我可以在运行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"}
...
我正在尝试在LibImport模型index.html.erb
的索引视图中使用命名路由“reimport_lib_import”,其中一部分如下所示:
...
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 %>
...
但结果是浏览器显示错误。错误的相关部分是:
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>
我的问题是:当我尝试使用它时,为什么Rails会抱怨reimport_lib_import
?事实上它在前一行航行lib_import
而没有投诉,但是对这一次的绊倒让我很困惑任何人都有任何想法?
答案 0 :(得分:1)
您应该使用
<td><%= link_to 'Show', lib_import_path(lib_import) %></td>
<td><%= link_to 'Re-import', reimport_lib_import_path(lib_import) %></td>
rake routes
中生成(显示)的名称需要跟_path
表示相对路径,或_url
表示完整网址。
其次,如果它是成员路径,则需要指定成员,在这种情况下:lib_import
本身。
希望这有帮助。