Question

I'm struggling with this issue for the past week or so, and I've tried everything I could think of so I need your help..! I'm using devise and devise invitable

I've created a page to edit user info like username, firstname, lastname...

# /controllers/settings_controllers.rb 
class SettingsController < ApplicationController   
  def info
    @user = current_user   
  end 
end

# /controllers/users_controllers.rb 
class UsersController < Devise::SessionsController   
  def update
    @user = User.find(current_user.id)
    if @user.update_attributes(user_params)
      redirect_to :back
    end
  end
end

# /views/settings/info.html.erb 
<%= form_for(@user) do |f| %> 
<%= render 'shared/error_messages' %> 
<%= f.label :username %> 
<%= f.text_field :username %> 
<%= f.label :firstname %> 
<%= f.text_field :firstname %> 
.... 
<% button_value = "Update" %> 
<% end %>

My routes :

  devise_for :users ,:controllers => { :invitations => 'users/invitations' }
  resources :users, only: [:edit, :update]
  devise_for :users, :skip => [:registrations]                                          
    as :user do
      get 'user/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'    
      put 'user' => 'devise/registrations#update', :as => 'user_registration'            
    end

  devise_scope :user do
    authenticated :user do
      root :to => 'aggregator#index'
    end
    unauthenticated :user do
      root :to => 'devise/sessions#new'
    end

    get "users/new" => "users#new"
    get "users/:id" => "users#show"
  end
  match 'settings/info' => 'settings#info', :as => 'info'

When I try to update the form, I have the following error (my user id is 1) :

Could not find devise mapping for path "/users/1"

Edit

So I've put resources :users, only: [:edit, :update] after devise_for :users, like suggested by @coletrain and error is gone. But it redirects to my profile /users/1 when I want to be redirected to /settings/info and more importantly, it does not update my info... My guess is that update method in users_controller is not reached.

Was it helpful?

Solution

In the routes.rb: add put "users/:id" => "users#update" inside devise_scope :user do ... end block.

Also: In user_controller update method, change @user.update_attributes(user_params) to @user.update_attributes(params["user"])

OTHER TIPS

I had the same problem. I think the simplest solution is this: Just use what devise gives you by default.

Routes:

devise_scope :user do

  get "account", to: "devise/registrations#edit"
  patch "account", to: "devise/registrations#update"
  put "account", to: "devise/registrations#update"
  delete "account", to: "devise/registrations#destroy"

end

/views/devise/registrations/edit.html.erb #generated by devise replace the path like this:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>

to this (because I named the routes "account" in this example)

<%= form_for(resource, as: resource_name, url: account_path, html: { method: :put }) do |f| %>

Note that you have to delete resource_name too. Otherwise there will be routing problems after you committed changes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top