[Rails] Add column to devise

Why i wrote

When you create user authentication with devise, you will have email and password from the beginning, but you need a name to register as a user. I always think, so take a note.

Premise

--Already added the devise gem --It must be rails g devise User

Add a column

When adding a column to the DB, it is common to write as follows. For now, give the migration file a name that tells you what the migration file does.

When naming it, if you write ʻAddColumnToUsers` and it means something like" add a column to the user table ", be careful because the name will be a problem when you add the next column. In the first place, I think that it is necessary to add columns = DB design, but I am still inexperienced in this area, so please tell me who is strong.

$rails generate migration Add Column name To Table name Column to add:Data type
$ rails generate migration AddNameToUsers name:string

XXXXXXXXXXX_add_name_to_users.rb


class AddNameToUsers < ActiveRecord::Migration[5.2]
  def change
    add_column :users, :name, :string
  end
end
$ rails db:migrate

Fix Controller and View

erb:registrations/new.html.erb


<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>

	#Add from here
 <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name, autofocus: true %>
  </div>
	#So far

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "new-password" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "new-password" %>
  </div>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

I want to be able to receive the information of the name that flew from View, so Add the following to application_controller.rb.

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

Write the following code in ʻapplication.html.erb. current_user` is one of the methods provided by devise, and you can get the information of the logged-in user.

erb:application.html.erb


<% if current_user.present? %>
  <p>Hello,<%= current_user.name %>San!</p>
<% else %>
  <p>Hello, Guest!</p>
<% end %>

If it is displayed well, it's OK!

スクリーンショット 2020-07-22 17.47.14.png

Recommended Posts

[Rails] Add column to devise
[Rails] Add strong parameters to devise
[Rails] How to install devise
[Rails] devise
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] Processing after adding a column to the devise table
Add binding.pry (rails)
Migration file to add comment to Rails table
Add foreign key to column with migrate
[Rails] Introducing devise
[Rails] Add a confirmation screen and a completion screen to devise membership registration.
[Rails] [Memo] When to add = to <%%> and when not
rails + devise + devise_token_auth
I want to add devise in Rails, but I can't bundle install
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to translate devise into Japanese
[Rails] How to log in with a name by adding a devise name column
[Rails] How to create a table, add a column, and change the column type
[Rails] Add page nation to table [For beginners]
The road to Japaneseizing Rails devise error messages
How to add / remove Ruby on Rails columns
Add a tag function to Rails. Use acts-as-taggable-on
How to conditionally add html.erb class in Rails
[Rails] Added in devise: username not added to database
Add files to jar files
How to write Rails
Introducing CircleCI to Rails
[Rails] devise helper method
[Rails] Customize devise validation
Introducing Bootstrap to Rails !!
Introduce Vue.js to Rails
Handle devise with Rails
How to uninstall Rails
[Rails] devise introduction method
[rails] About devise defaults
[rails] How to use devise helper method before_action: authenticate_user!
Introduce devise in Rails to implement user management functionality
[Ruby on Rails] How to change the column name
[Ruby on Rails] Change URL id to column name
[Rails] How to change the column name of the table
[Rails] How to edit and customize devise view and controller
I want to add a reference type column later
[Rails 6] Add images to seed files (using Active Storage)
[Rails] Add Tags that are related to has_many to Devise User (addition of function to follow tags)
[rails] How to post images
[Rails] Introduction of devise Basics
What is Rails gem devise?
Add an icon to the header link using Rails fontawesome
How to add ActionText function
[Rails] gem devise installation flow
[Rails] How to use enum
[Ruby on Rails] Add a column with a foreign key constraint
I was addicted to setting default_url_options with Rails devise introduction
4 Add println to the interpreter
How to use rails join
(For beginners) [Rails] Install Devise
[Rails] How to delete images uploaded by carrierwave (using devise)
[Rails] Add / Remove Forms (cocoon)
How to terminate rails server
How to write Rails validation