Rails 4 Create a name, a surname, not getting into the database

I am setting up an application on my 4 rails application. I followed this tutorial https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address and added a username value. I also need a first and last name, so I suggested that this is something close to the textbook. I followed some parts and skipped the authentication parts and updated the views. It works somewhat. When fields are registered, and when you fill them in, they are checked, but not entered into the database. It just shows NULL for the first and last name, but the username really works. Here are the steps I took.

  • I completed the entire tutorial (except for the last part about gmail and me.com).

  • I added the First and Last Name fields:

I executed the commands

rails generate migration add_firstname_to_users first_name:string:uniq
rails generate migration add_lastname_to_users last_name:string:uniq

Then it turned out rake db:migrate. Then I added the fields to the application controller to allow the fields. Here is my fullapplication_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_filter :configure_permitted_parameters, if: :devise_controller?
  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :username, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :username, :email, :password, :password_confirmation, :current_password) }
  end
end

Then I added the first and last name to attr_accessor. Here is my complete model user.rb.

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  attr_accessor :login, :first_name, :last_name

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]

  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
    else
      where(conditions).first
    end
  end
end

Then I updated my presentation and added <%= f.text_field :first_name %>and <%= f.text_field :last_name %>new records of registration and registration.

Fields appear and have no errors when submitting the form. They just do not update the database. I manually added the name to the MYSQL database via PHPMyAdmin, and then went to the edit page and grabbed it correctly. It would be great if you could help. Thank you :)

+3
source share
1 answer
+3

All Articles