jeudi 2 septembre 2021

How can add customer field in devise user table in rails

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 %>

Aucun commentaire:

Enregistrer un commentaire