Reputation: 2689
I setup a devise on my rails 4 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 the username value. I also wanted First Name and Last Name, so I assumed it was something close to the tutorial. I followed SOME of the parts and skipped the authentication parts, and update the views. It is somewhat working. When registering the fields show up and when you fill up, they pass all check but they DO NOT get entered in the DB. It just shows up NULL for the First name and Last name, but username is actually working. Here are the steps that I did.
I followed the whole tutorial (except for the last part about gmail and me.com).
I added the First Name and Last Name fields:
I ran 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 did rake db:migrate
. Then I added the fields to application controller to permit the fields. Here is my full application_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 firstname and lastname to the attr_accessor
. Here is my full user.rb
model.
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 views and added <%= f.text_field :first_name %>
and <%= f.text_field :last_name %>
to the registrations new and registrations edit views.
The fields show up and have no errors when submitting the form. They just do not update the DB. I added the name manually in the MYSQL database through PHPMyAdmin and then went to the edit page and it grabs it correctly. It would be great if you could help. Thanks! :)
Upvotes: 3
Views: 860
Reputation: 2491
Try to remove it from attr_accessor because attr_accessor works like instance variable
http://apidock.com/ruby/Module/attr_accessor
Upvotes: 3