ruby in rails在设计中确认电子邮件时出错

时间:2016-12-11 03:51:37

标签: ruby-on-rails ruby devise ruby-on-rails-5

我在rails5上使用devise确认用于登录的电子邮件地址。一切顺利,直到我点击确认链接。然后它向我显示了这个错误:

  

Devise中的NameError :: ConfirmationsController #show

     

未定义的局部变量或方法`signin'代表#<类:0x007fb1cbe56b48>

这是导致错误的代码:

        conditions = warden_conditions.dup
        where(conditions).where(["lower(username) = :value OR lower(email)
            = :value", { :value => signin.downcase }]).first

end

这是我上面代码所属的模型:

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :confirmable
attr_accessor :signin

validates :username, :uniqueness => {:case_sensitive => false}

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

有人可以帮我吗?如果你给我一些关于这种方法是如何工作的信息呢?

2 个答案:

答案 0 :(得分:0)

我在这里发表你的评论。我不知道你如何处理登录,但我这样做的方式是会话控制器和会话助手:

#Session Controller

 def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:email].downcase)
      log_in user
      redirect_back_or user
      flash[:success] = "You are logged in"
    else
      flash[:danger] = "Wrong email or password"
      render 'new'
    end
  end

" log_in(用户)"我使用的方法在Session Helper中定义。

#Session Helper

  def log_in(user)
    session[:user_id] = user.id
  end

答案 1 :(得分:0)

首先,attr_accessor定义了一个实例方法,而不是一个类方法,因此如果在类signin之类的类方法中调用self.find_first_by_auth_conditions,那么undefined local variable or method 'signin' for #< Class:0x007fb1cbe56b48>将会抛出find_for_database_authentication错误。

其次,您的def self.find_for_database_authentication(warden_conditions) conditions = warden_conditions.dup if login = conditions.delete(:login) where(conditions.to_h).where([ "lower(username) = :value OR lower(email) = :value", { :value => login.downcase } ]).first elsif conditions.has_key?(:username) || conditions.has_key?(:email) where(conditions.to_h).first end end 方法不完整。您应该根据this example

let data = [
{"category":"category1","months":{"1":34,"2":67,"3":29}},
{"category":"category2","months":{"1":34,"2":627,"3":292}},
{"category":"category3","months":{"1":46,"2":665,"3":129}},
{"category":"category4","months":{"1":624,"2":667,"3":629}},
{"category":"category5","months":{"1":32,"2":637,"3":299}},
];

let total = {"category":"total","months":{}};

data.forEach(category => {
      for(let prop in category.months){
          if (total.months[prop]){
            total.months[prop] += category.months[prop];
          }
          else{
            total.months[prop] = category.months[prop];
          }
      }
});

data.push(total);

我不确定您是否已将该示例用作基础,但如果您解释修改它的原因是什么,那就太棒了。我没有理由,使用那段代码。