Rails: Nested resources

天大地大妈咪最大 提交于 2019-12-11 20:17:53

问题


The following path throws up an error:

 = link_to 'Subscribers', user_subscribers_path(current_user)

undefined method `user_subscribers_path' for <#:0x007f9b240b3148>

I am not sure why.

I have defined my routes as follows:

  resources :users, :only => [:show, :index], :has_many => :subscribers, :shallow => true

Thanks!

EDIT rake routes does not show anything particularly useful. The only two lines with subscribers are:

users GET    /users(.:format)               users#index {:has_many=>:subscribers}
user GET    /users/:id(.:format)           users#show {:has_many=>:subscribers}

回答1:


You need to define resource subscribers in routes files as follows

resources :users do 
 resources :subscribers
end

this will create the needed path helper for your resource

For the shallow routes you can use

 map.resources :users, :shallow => true do |user|
  user.resources :subscribers 
 end


来源:https://stackoverflow.com/questions/12375361/rails-nested-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!