0
votes

I have developed a basic rails 3.2 app using devise (2.2.3) for authentication. Now I need to add support for the user account/profile settings. The additional attributes (of profile/account) that can be updated by the end user are part of the User model.

I need suggestion on how this can be supported? Which action of the UsersController would meet the requirement? I added an edit action in the UsersController for the same. When I run 'rake routes', I get the following

    new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
        user_session POST   /users/sign_in(.:format)       devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
       user_password POST   /users/password(.:format)      devise/passwords#create
   new_user_password GET    /users/password/new(.:format)  devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                     PUT    /users/password(.:format)      devise/passwords#update

cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel user_registration POST /users(.:format) devise/registrations#create new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show root / home#index

When the edit form is submitted, the form sends a message to "/users/1/edit" with PUT and I get the routing error that No route matches [PUT] "/users/1/edit"

Is this the right way to modify the user settings? Should the form be posted with "PUT"? If so, how do I make a route entry with PUT instead of GET as above?

Thanks in advance.

2

2 Answers

0
votes

If you want users to be able to update "their" User object then you will need to setup a routing entry for them in your routes.rb. Something like:

resource :users => [:edit, :update]

Your form should be PUTing to /users/:id not /users/:id/edit.

Once this is setup you will be able to use form helpers to create forms for users to edit User objects. You will want to consider security here and make sure they don't update fields they should not have access to.

Rails doing a PUT request is correct as one of the main tenants of rails is to try to make everything RESTful.

0
votes
edit_user GET    /users/:id/edit(.:format)      users#edit

This is fine because going to users/:id/edit is simply getting the form. The form is then filled out and the act of pressing "submit" will make a PUT request to /users/:id. You're just missing this second part.

Jeremy's answer should fix the routes problem.