When you have introduced devise but want to add more columns

Try using devise

I introduced devise, but isn't there a time when this is the only column?

This time I will explain how to add columns! !!

1. Add a column to the users table

This time we will add a name column. Execute the following command in the terminal.

$ rails g migration AddNameToUsers name:string

$ rails db:migrate

2. Edit application_controller.rb

Let's edit application_controller.rb as follows. The name column is now saved when you register as a user.

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
  end
end

* As it is, the name column is included only at the time of registration, so even if you change the name by editing, it will not be changed.

3. Let's make it possible to edit the columns added at the time of editing!

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
    <!--Let's add the corresponding description at the time of editing as follows-->
    devise_parameter_sanitizer.permit(:account_update, keys: [:name])
  end
end

The data is now reflected when editing: point_up_tone2:

I hope you found this article helpful: pray_tone2:

Recommended Posts

When you have introduced devise but want to add more columns
If you want to modify database columns etc.
When you want to bind InputStream in JDBI3
When you want to use the method outside
[Ruby] When you want to replace multiple characters
[rails] After option useful when you want to change the order of DB columns
When you want to dynamically replace Annotation in Java8
When you want to explicitly write OR or AND with ransack
When you want to change the MySQL password of docker-compose
docker-compose.yml when you want to keep mysql running with docker
lombok.config when you want to pass @Qualifier to @RequiredArgsConstructor with lombok
Delegate is convenient to use when you want to reuse parts
When you deploy to Heroku but it doesn't reflect well
[Rails] Add column to devise
ProxyFactory is convenient when you want to test AOP in Spring!
[Docker] Magic command when you want to wipe out none image
[Rails] I want to add data to Params when transitioning with link_to
Practice to use when you want to execute different processing groups serially
When you want to add a string type column with a limited length with the `rails generate migration` command
How to add columns to a table
[Rails] Add strong parameters to devise
If you want to use Oracle JDK 11 from September (add Amazon Corretto)
When you want Rails to disable a session for a specific controller only
Summary of means when you want to communicate with HTTP on Android