开发者

Rails: No route matches {:controller=>"settings", :action=>"edit"}

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, t开发者_运维百科his 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.


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.


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)


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!


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜