简体   繁体   中英

Nested Resource with Devise - Rails3

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM