Nested Resource with Devise - Rails3

独自空忆成欢 提交于 2021-02-06 20:05:42

问题


I'm trying to setup a profiles controller to work with devise registered members.

I've created the Profile model & controller and added a one-to-one relationship by adding belongs_to :member to my profile.rb model and has_one :profile in my member.rb model.

To create the nested resource I used:

devise_for :members, :path => "accounts",:path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }

resources :members do
  resources :profiles
end

Calling rake routes outputs

     member_profiles GET    /members/:member_id/profiles(.:format)          {:action=>"index", :controller=>"profiles"}
                     POST   /members/:member_id/profiles(.:format)          {:action=>"create", :controller=>"profiles"}
  new_member_profile GET    /members/:member_id/profiles/new(.:format)      {:action=>"new", :controller=>"profiles"}
 edit_member_profile GET    /members/:member_id/profiles/:id/edit(.:format) {:action=>"edit", :controller=>"profiles"}
      member_profile GET    /members/:member_id/profiles/:id(.:format)      {:action=>"show", :controller=>"profiles"}
                     PUT    /members/:member_id/profiles/:id(.:format)      {:action=>"update", :controller=>"profiles"}
                     DELETE /members/:member_id/profiles/:id(.:format)      {:action=>"destroy", :controller=>"profiles"}

Going to localhost:3000/members/1/profiles in the browser correctly routes me to the profiles#index page, but when I try calling member_profiles_path it fails with the following error:

No route matches {:controller=>"profiles"}

Can anyone see where I'm going wrong here? I'm using rails 3.0.3.


回答1:


Since this is a has_one association, have you tried using resource rather than resources?

resources :members do
  resource :profile
end

You should then be able to do something like this:

link_to "Profile", member_profile_path(@member)


来源:https://stackoverflow.com/questions/5785684/nested-resource-with-devise-rails3

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