Dercni
Dercni

Reputation: 1224

Devise hide the user id from the url

When editing a user in devise the edit_user_registration_path route is:

edit_user_registration GET    /users/edit(.:format)                                  devise/registrations#edit

I would like to have a route to edit a user's profile without disclosing the user_id in the url however my route is:

edit_profile GET    /profiles/:id/edit(.:format)                           profiles#edit

How can I create a similar route and hide the user_id from the url?

EDIT

Ok, I worked this one out... partially using: http://guides.rubyonrails.org/routing.html#singular-resources

I have created a singular resource in my routes.rb file

resource :profile

This now allows me to view the current user's profile with /profile and edit the current user's profile with /profile/edit

However when I edit the current user's profile and click update I am redirected to /profile.1? /profile.2 /profile.3 and so on all display the current user's profile.

Where does the profile.X come from and how do I simply redirect back to /profile after I update?

My update action is simply:

def update
  @profile.region = set_region(params[:postal_code], params[:country])
  @profile.assign_attributes(profile_params)
  @profile.save
  @profile.user.update_attributes (user_params)
  respond_with(@profile)
end

Upvotes: 1

Views: 999

Answers (1)

Hoa
Hoa

Reputation: 3207

Your question is

Where does the profile.X come from and how do I simply redirect back to /profile after I update?

Run the following command

$ rake routes | grep profile
     profile POST   /profile(.:format)         profiles#create
 new_profile GET    /profile/new(.:format)     profiles#new
edit_profile GET    /profile/edit(.:format)    profiles#edit
             GET    /profile(.:format)         profiles#show
             PATCH  /profile(.:format)         profiles#update
             PUT    /profile(.:format)         profiles#update
             DELETE /profile(.:format)         profiles#destroy

As you can see profile_path receives no parameter. A parameter passed to profile_path will be interpreted as format. You can try the following in Rails console

> app.profile_path
 => "/profile" 
> app.profile_path(1)
 => "/profile.1" 
> app.profile_path("json")
 => "/profile.json" 

Upvotes: 0

Related Questions