使用 Devise 嵌套资源 - Rails3

发布于 2024-11-03 05:10:47 字数 1724 浏览 0 评论 0原文

我正在尝试设置一个配置文件控制器来与设备注册成员一起使用。

我已经创建了 Profile 模型 &控制器,并通过将 belongs_to :member 添加到我的 profile.rb 模型和 has_one :profile< 来添加一对一关系 /code> 在我的 member.rb 模型中。

要创建我使用的嵌套资源:

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

resources :members do
  resources :profiles
end

调用 rake paths 输出

     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。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半城柳色半声笛 2024-11-10 05:10:47

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

resources :members do
  resource :profile
end

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

link_to "Profile", member_profile_path(@member)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文