I would like to hide or mask example.com/users/$ID/edit to be example.com/profile/edit.
config/routes.rb
resources :users
app/controllers/users_controller.rb
def edit
@user = User.find(params[:id])
end
I’ve tried adding the route
match '/profile/edit' => 'users#edit', :as => :edit_profile
but, when I visit example.com/profile/edit, the edit method complains about not being able to find the user’s ID.
Is there a way I can mask the ID from the browser?
If you’re trying to edit the current user’s profile you could use the following in your edit action:
This will first try to look up the user by the id parameter returning nil if it can’t find the user and if the return value is nil it will set
@userto the return value of thecurrent_userhelper method. This assumes you’re using something like Devise which provides thecurrent_usermethod to get the currently logged in user.One other note. You should change match in your route to get to specify that only get requests are valid for the edit action.
If you’re trying to prevent users from editing other people’s profiles you need something like the following after you load the user:
This will keep the current user from editing another user’s profile.