Rails: transition to the user action of the controller

It’s very difficult for me to understand the routes, and I hope someone can help me.

Here is my user controller

class SettingsController < ApplicationController before_filter :authenticate_user! def edit @user = current_user end def update @user = User.find(current_user.id) if @user.update_attributes(params[:user]) # Sign in the user bypassing validation in case his password changed sign_in @user, :bypass => true redirect_to root_path else render "edit" end end end 

and I have the settings of the /edit.html.erb file and my link

 <li><%= link_to('Settings', edit_settings_path) %></li> 

Route

 get "settings/edit" 

not working for this because then i get

 undefined local variable or method `edit_settings_path' for #<#<Class:0x00000001814ad8>:0x00000002b40a80> 

Which route do I need to give? I can not understand. If I put "/ settings / edit" instead of the path that it messed up as soon as I am on another resource page, because the name of the resource is placed BEFORE the settings / editing

thanks

+6
source share
3 answers

The following should be done:

 get 'settings/edit' => 'settings#edit', :as => :edit_settings # you can change put to post as you see fit put 'settings/edit' => 'settings#update' 

If you use /settings/edit directly in the link, you should have no problem adding another resource name to the path. However, without a leading slash, i.e. settings/edit , this problem may occur.

The reason that edit_settings_path does not work may be because you did not declare a named route. You should use the :as option to determine how you will generate this path / URL.

+16
source

If you want to explicitly define a route, you should use something like

 get 'settings/edit' => 'settings#edit', :as => edit_settings 

This statement determines that when receiving a GET request for installation / editing, call the settingsController # editing method, and these views can reference this link using 'edit_settings_path'.

Take some time to read the Rails manual on routing . This explains routing better than any other link.

Also keep in mind the rake routes task, which lists information about all the routes defined in your application.

+4
source

Change the route to

 resources :settings 

And that probably just works.

+1
source

Source: https://habr.com/ru/post/918281/


All Articles