Rails:没有路线匹配 {:controller=>"settings", :action=>"edit"}
我在用户和设置模型之间有一个 has_one 关联。我还有具有编辑和更新操作的 SettingsController。在首页上,我有一个编辑设置的链接:
<%= link_to (settings_path(current_user.setting)), do %>
..
<% end %>
这导致 ActionController::RoutingError 没有路由匹配 {:controller=>"settings", :action=>"edit"} ..尝试时显示首页。
我有点绞尽脑汁思考为什么会发生这种情况。使用 Devise 进行用户认证,这个 current_user 应该是一个全局变量。
以下是在routes.rb中定义路由的方式:
resources :setting, :only => [:edit, :update]
match '/settings/:id' => "settings#edit", :controller => :setting, :as => :settings
这是rake路由返回的内容:
edit_setting GET /setting/:id/edit(.:format) {:action=>"edit", :controller=>"setting"}
setting PUT /setting/:id(.:format) {:action=>"update", :controller=>"setting"}
settings /settings/:id(.:format) {:controller=>"settings", :action=>"edit"}
另一个猜测是,当使用has_one关联时,控制器名称(SettingsController)应该是单数,而不是复数。由于某些奇怪的原因,Rails 没有注意到我的控制器,尽管它非常存在。
感谢帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用复数形式的名称:到处设置。
Rails 自动以复数形式命名模型。在您的示例中,您有一个奇怪的复数和单数名称组合,请检查一下。全部应该是复数。
try with the name in plural: settings everywhere.
Rails automagically names in plural the models. In your example you have a weird mix of names in plural and singular, check it out. All should be plural.
好吧,首先为什么你使用自己的控制器而不是设计来为用户进行设置?
位于
edit_user_registration_path
:as
参数中定义了它另一个注释是,如果你正在使用
resources :设置,:仅=> [:编辑,:更新]
你为什么使用下一行?这条路径,我的意思是,如果你这样声明它,你可以使用
edit_setting_path(id)
Ok, firts why are you using your own controller for settings for a user instead of devise ?
that is on
edit_user_registration_path
:as
parameterAnother comment is , if you are using
resources :setting, :only => [:edit, :update]
why are you using the next line ? and that path, I mean, if you declaring it like that, you could use
edit_setting_path(id)
啊,我找到了问题的根源。谢谢各位,你们给了我一些想法。问题是,我正在迁移用户模型以开始使用设置模型,而我使用的用户没有任何设置(这有点奇怪,因为我在 Rails 控制台中创建了它,一切看起来都很好)。所以设置对象为零,昨天这让 Rails 发疯了。我第一次看到各种各样的错误。
是的,该设置的资源路由应该是单一的,因为它使用的是 has_one 关联。在routes.rb 中的第二行中,我尝试使用更简单的网址(如 .../settings )获取用户的所有设置。
我也尝试过使用 Devise 的 edit_user_registration_path,但上次出了问题。可能需要再考虑一下。
谢谢大家!
Ahh, I found the root problem for this. Thanks, guys, you gave me some ideas. The thing was that I was migrating User model to start using Settings model, and the user I was using didn't had any Setting (which was kinda weird, since I created it in the rails console and it all looked fine). So the setting object was nil, and that was driving rails crazy yesterday. I saw all kinda of errors for the first time.
Yes, the resource route for the setting should be singular, since it is using has_one association. With my second line in the routes.rb I was trying to get user all his settings with a more simple url like .../settings .
I tried using Devise's edit_user_registration_path too, but something went wrong last time. Probably have to give it another thought.
Thanks to everyone!
在 link_to 方法调用中,尝试使用setting_path(单数)而不是settings_path。
In your link_to method call, try using setting_path (singular) rather than settings_path.