sja
sja

Reputation: 347

Allowing user to edit account without the :id param in the url

Hi I'd like to accomplish having a URL that's

users/edit

instead of the current

users/7/edit

I have my own auth system built upon omniauth. Thus I store their user_id in a session. How would I go about accomplishing this task?

Upvotes: 1

Views: 170

Answers (1)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Assuming that you use current_user, even if you are using something else just replace current_user with your method, I am using current_user here, follow these steps,

  1. Create an action, I would name it edit_user in your users controller

    def edit_user
      @user = current_user # or User.find(session[:user_id])
    end
    
  2. Add routes to routes.rb

    get "/users/edit" => "users#edit_user"
    

You are done, you can use the above route anywhere in the application, you can also name the route if needed.

OR, if you don't want to define a new action and want to use the existing edit action, do this

  1. Remove routes for edit from the default resources routes and then manually define it. In this way, you can use the existing edit action

    resources :users, except: [:edit]
    get "/users/edit" => "users#edit"
    

Hope this helped!

Upvotes: 3

Related Questions