允许用户使用Devise

时间:2015-07-11 00:39:14

标签: ruby-on-rails login devise username

问题

我关注tutorial on Devise's Wiki page,现在我的登录页面已损坏。我不断收到错误消息,说“电子邮件或密码无效。”#39; user_name和email都不能用于登录。

注意:我的用户名字段为user_name而非username,如教程中所示。

我错过了什么吗?输错了什么?在大多数情况下,我剪切和粘贴。

代码

应用程序控制器:

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

用户模型:

## 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 for authenticating by either user_name or email
    # This is in addition to a real persisted field like '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

设计初始化程序(取消注释并仅更改此行):

## app/config/initializers/devise.rb

config.authentication_keys = [ :login ]

登录视图:

## 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)) do |f| %>

  <%= f.alert_message "Please fix the 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" %>

设计错误标签(因为我有这些行,所以不需要改变):

## app/config/locales/devise.en.yml

invalid: "Invalid %{authentication_keys} or password."
...
not_found_in_database: "Invalid %{authentication_keys} or password."

提前感谢您提供任何帮助。

更新问题

使用的版本

Ruby:ruby 2.2.1p85(2015-02-26修订版49769)[x86_64-darwin14]

Rails:Rails 4.2.0

设计3.4.1

新的ApplicationController代码

詹姆斯·米兰提供了答案,因为我忘了重启我的服务器。但是,一旦我这样做,ApplicationController代码开始出错,提供2次错误重复3次。

/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

我像这样更改了应用程序控制器,错误消失了,一切正常:

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

现在,为什么其他代码不起作用,我不知道,但这是另一个问题。截至目前,这个问题得到了回答。我刚刚更新了此部分,以防其他人稍后遇到相同的问题或问题。

1 个答案:

答案 0 :(得分:1)

我通过了你的代码 - 看起来不错;如果我有50个或更多的代表,这将是一个评论,但你确实重新启动了服务器,对吗?否则初始化程序将无法生效。也许那就是它?