嵌套资源与Devise - Rails3

时间:2011-04-26 03:47:49

标签: ruby-on-rails devise

我正在尝试设置配置文件控制器以与设计注册成员一起工作。

我创建了Profile模型&amp;控制器并添加了one-to-one relationship,将belongs_to :member添加到我的 profile.rb 模型和{strong> member.rb 模型中的has_one :profile。< / p>

创建我使用的嵌套资源:

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

resources :members do
  resources :profiles
end

调用rake routes输出

     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"}

在浏览器中转到localhost:3000/members/1/profiles可以正确地将我转到profiles#index页面,但当我尝试拨打member_profiles_path时,它会失败,并显示以下错误:

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

谁能看到我在哪里错了?我正在使用rails 3.0.3。

1 个答案:

答案 0 :(得分:10)

由于这是一个has_one关联,您是否尝试过使用资源而不是资源?

resources :members do
  resource :profile
end

然后你应该能够做到这样的事情:

link_to "Profile", member_profile_path(@member)