"edit"}" />

Rails:没有路线匹配 {:controller=>"settings", :action=>"edit"}

发布于 2024-11-19 16:09:09 字数 1111 浏览 3 评论 0 原文

我在用户和设置模型之间有一个 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 没有注意到我的控制器,尽管它非常存在。

感谢帮助。

I have a has_one association between user and setting model. I have also SettingsController with edit and update actions. On front page I have a link to edit settings:

<%= link_to (settings_path(current_user.setting)), do %>
..
<% end %>

This causing ActionController::RoutingError No route matches {:controller=>"settings", :action=>"edit"} ..when trying to display front page.

I kinda stuck banging my head around why this is happening. Using Devise for user authentication, this current_user should be a global variable.

Here is how routes are defined in routes.rb:

  resources :setting, :only => [:edit, :update]  
  match '/settings/:id' => "settings#edit", :controller => :setting, :as => :settings

Here is what rake routes is returning:

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

Another guess is that controller name (SettingsController) should be singular, not plural when using has_one association. For some strange reason Rails is not noticing my controller, even though it is very present.

Help is appreciated.

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

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

发布评论

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

评论(4

不知所踪 2024-11-26 16:09:09

尝试使用复数形式的名称:到处设置。

<%= link_to (settings_path(current_user.settings)), do %>
...

match '/settings/:id' => "settings#edit", :controller => :settings, :as => :settings
...

Rails 自动以复数形式命名模型。在您的示例中,您有一个奇怪的复数和单数名称组合,请检查一下。全部应该是复数。

try with the name in plural: settings everywhere.

<%= link_to (settings_path(current_user.settings)), do %>
...

match '/settings/:id' => "settings#edit", :controller => :settings, :as => :settings
...

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.

避讳 2024-11-26 16:09:09

好吧,首先为什么你使用自己的控制器而不是设计来为用户进行设置?
位于 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

  • Your path it's ok, it has to be in plural because you defined it in the :as parameter

Another 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)

月亮邮递员 2024-11-26 16:09:09

啊,我找到了问题的根源。谢谢各位,你们给了我一些想法。问题是,我正在迁移用户模型以开始使用设置模型,而我使用的用户没有任何设置(这有点奇怪,因为我在 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!

以为你会在 2024-11-26 16:09:09

在 link_to 方法调用中,尝试使用setting_path(单数)而不是settings_path。

<%= link_to (setting_path(current_user.setting)), do %>

In your link_to method call, try using setting_path (singular) rather than settings_path.

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