Search code examples
ruby-on-railsdevise

Why doesn't the Devise registrations edit action need params?


I am learning a Rails course, and I have this in my routes

devise_for :users,
             path: '',
             path_names: { sign_up: 'register', sign_in: 'login', edit: 'profile', sign_out: 'logout' },
             controllers: { omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations' }

This will generates routes like

 edit_user_registration GET      /profile(.:format)      registrations#edit

In my view I just need to use edit_user_registration_path while normally it should be sth like user_path(:id). If I write edit_user_registration_path(1) it will redirect to .../profile.1

As I know normal Rails edit route should have params[:id]. Why in this case it doesn't need and how edit_user_registration_path(1) generates .../profile.1?


Solution

  • The routes don't need and don't take an ID parameter since they act on the current user which is stored in the session and not passed through the parameters.

    Similarly if you wanted to create a controller that deals with items belonging to the current user you could do:

    scope :user do
      resources :items, controller: :user_items
    end
    
    class UserItemsController
      before_action :authenticate_user
      # GET /user/items
      def index
        @items = current_user.items
      end
    end
    

    If you instead where building something like an admin interface where you can edit other users on the system an id param would be necessary.