ruby on rails - Allow users to sign in using their username or email address with Devise -
problem
i followed tutorial on devise's wiki page , login page broken. continually error saying 'invalid email or password.' neither user_name nor email work logging in.
note: user name field user_name
not username
in tutorial.
did miss something? type wrong? part cut , paste.
code
application controller:
## app/controllers/application_controller.rb class applicationcontroller < actioncontroller::base protect_from_forgery with: :exception before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit( :email, :birthday, :password,:password_confirmation, :remember_me) } devise_parameter_sanitizer.for(:sign_in) { |u| u.permit( :login, :user_name, :email, :password, :remember_me ) } devise_parameter_sanitizer.for(:account_update) { |u| u.permit( :first_name, :last_name, :user_name, :birthday, :address_1, :address_2, :city, :state, :zip_code, :facebook, :twitter, :google_plus, :instagram, :pinterest ) } end end
user model:
## app/models/user.rb class user < activerecord::base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable validates :birthday, :presence => true validates :user_name, :presence => true, :uniqueness => { :case_sensitive => false } # virtual attribute authenticating either user_name or email # in addition real persisted field 'user_name' attr_accessor :login def self.find_for_database_authentication(warden_conditions) conditions = warden_conditions.dup if login = conditions.delete(:login) where(conditions.to_h).where(["lower(user_name) = :value or lower(email) = :value", { :value => login.downcase }]).first else where(conditions.to_h).first end end end
devise initializer (uncommented , changed line):
## app/config/initializers/devise.rb config.authentication_keys = [ :login ]
login view:
## app/views/devise/sessions/new.html.erb <% @title="login" %> <h2>log in</h2> <%= bootstrap_form_for(resource, as: resource_name, url: session_path(resource_name)) |f| %> <%= f.alert_message "please fix errors below." %> <%= f.text_field :login, autofocus: true %> <%= f.password_field :password %> <% if devise_mapping.rememberable? -%> <%= f.check_box :remember_me %> <% end %> <%= f.primary "log in" %> <% end %> <%= render "devise/shared/links" %>
devise's error labels (didn't need change have these lines):
## app/config/locales/devise.en.yml invalid: "invalid %{authentication_keys} or password." ... not_found_in_database: "invalid %{authentication_keys} or password."
thank in advance can give.
update question
versions used
ruby: ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
rails: rails 4.2.0
devise 3.4.1
new applicationcontroller code
james milani provided answer in forgot restart server. but, once did that, applicationcontroller code started error providing 2 errors repeated 3 times.
/app/controllers/application_controller.rb:51: syntax error, unexpected '|', expecting '}' { |u| u.permit( :email, :birthda... ^ /app/controllers/application_controller.rb:51: syntax error, unexpected '}', expecting keyword_end
i changed application controller , errors went away , works:
## app/controllers/application_controller.rb class applicationcontroller < actioncontroller::base protect_from_forgery with: :exception before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) << :birthday devise_parameter_sanitizer.for(:sign_in) << :login devise_parameter_sanitizer.for(:account_update) << :first_name devise_parameter_sanitizer.for(:account_update) << :last_name devise_parameter_sanitizer.for(:account_update) << :user_name devise_parameter_sanitizer.for(:account_update) << :birthday devise_parameter_sanitizer.for(:account_update) << :address_1 devise_parameter_sanitizer.for(:account_update) << :address_2 devise_parameter_sanitizer.for(:account_update) << :city devise_parameter_sanitizer.for(:account_update) << :state devise_parameter_sanitizer.for(:account_update) << :zip_code devise_parameter_sanitizer.for(:account_update) << :facebook devise_parameter_sanitizer.for(:account_update) << :twitter devise_parameter_sanitizer.for(:account_update) << :google_plus devise_parameter_sanitizer.for(:account_update) << :instagram devise_parameter_sanitizer.for(:account_update) << :pinterest end end
now, why other code doesn't work, not know, question. of now, question answered. have updated section in case else has same question or issue later.
i made pass through code--looks good; if had 50 or more rep comment, did restart server, right? otherwise initializer won't come effect. maybe that's it?
Comments
Post a Comment