Home > OS >  How can add customer field in devise user table in rails
How can add customer field in devise user table in rails

Time:09-17

i am new in ror. I am trying to add two column in devise user table it show the error in html page i don't know how can i fix this issue. I am follow the youtube tutorial i don't know how but his code wrok perfectly and my code showing error. I even add the fields in migration file and run db migrate but nothing change

undefined method `last_name' for #<User id: nil, email: "", created_at: nil, updated_at: nil>

application controller.erb

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

   protected

   def configure_permitted_parameters
       devise_parameter_sanitizer.permit(:sign_up, keys: [:role_id, :first_name, :last_name])
   end
end

Schema.rb

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

html

  <h2>Sign up</h2>

  <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>

  <%= render "devise/shared/error_messages", resource: resource %>

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

  <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </div>

  <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name %>
  </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 %>

CodePudding user response:

your migration will look something like this

class AddLastNameToUser < ActiveRecord::Migration
  def change
    add_column :users, :last_name, :string
    
  end
end

once it does, run the following commands in your terminal:

rails db:drop
rails db:create
rails db:migrate

this will drop your current db schema, create a new one, and run any migrations you have. there are short hand versions to accomplish the same thing, but better to understand the basics first

  • Related