ruby-on-rails - rails 路由显示操作嵌套资源

标签 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/元素

这将启动元素 Controller 的索引操作,就好了。
但是如果我将 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 route 显示了?
我究竟做错了什么 ?

最佳答案

您需要为专辑元素路径提供两个必要的参数:

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

关于ruby-on-rails - rails 路由显示操作嵌套资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12800734/

相关文章:

html - 从 CSS 链接的强大图像 URL?

ruby-on-rails - 何时在 Rails 3 中使用虚拟属性或以散列形式传递数据

sql - 通过 'has_many'获取属性平均值

c++ - Qt 资源文件解决方法

grails - 耐火 Material 处理不当

Grails 错误?更新操作忽略rejectValue

javascript - 错误 : Unknown provider: $resourceProvider <- $resource <- myservice AngularJS services

ruby-on-rails - 无方法错误 : undefined method `confirm!' for #<User

ruby-on-rails - 渲染状态 : 403 causes "Missing template" error

.net - .Net 中的 WebClient 不释放套接字资源