rails routing show action嵌套资源

时间:2012-10-09 13:06:16

标签: ruby-on-rails resources controller routes nested

是的问题是,我创建了一个这样的嵌套资源:

resources :albums do 
  resources :elements
end

和rake routes命令显示:

album_element GET /albums/:album_id/elements/:id(.:format) elements#show

所以当我在 ... /专辑/ 1 我可以前往 ... /专辑/ 1 /元件

这会启动元素控制器的索引操作,就好了。 但是,如果我将index.html.erb编辑为

<%= link_to 'Show', album_element_path %>

我收到了这样的错误:

Started GET "/albums/1/elements" for 176.221.47.67 at Tue Oct 09 14:25:39 +0200 2012
Processing by ElementsController#index as HTML
Parameters: {"album_id"=>"1"}
Rendered elements/index.html.erb within layouts/application (9.2ms)
Completed 500 Internal Server Error in 123ms

ActionController::RoutingError (No route matches {:controller=>"elements", :action=>"show"}):
app/views/elements/index.html.erb:29:in `_app_views_elements_index_html_erb___13604879__168097178'
app/views/elements/index.html.erb:18:in `each'
app/views/elements/index.html.erb:18:in `_app_views_elements_index_html_erb___13604879__168097178'
app/controllers/elements_controller.rb:7:in `index'

所以它说没有路线匹配......但实际上我的rake路线显示了吗? 我做错了什么?

1 个答案:

答案 0 :(得分:7)

您需要为album_element_path提供两个必要的参数:

<%= link_to 'Show', album_element_path(@album, @element) %>