1
votes

I have created a User model through the Devise gem that allows email, first_name, and password upon registration.

 def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :first_name
 end

I would like to save other attributes such as last_name, city, etc to the User model. I have ran the migrations and see these attributes in my schema.

However when I am on the user/edit page and try to save, the new attributes are not saving.

I have run the command to edit the devise controllers, but confused.

rails generate devise:controllers users

Do I still need to create a UsersController < ApplicationController in order to accept other attributes into the User model during an edit/update?

Then I could just permit all when trying to update

def user_params
  params.require(:user).permit!
end

Thanks

2

2 Answers

1
votes

Try this:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:user) << :first_name
  end
end
1
votes

Add the following filter to the application controller:

before_action :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit( :first_name, :email, :password, :password_confirmation) }
  end

This is for sign up. To update the user informaiton add following line of code within the configure_permitted_parameters filter.

devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :email, :password, :password_confirmation, :current_password) }