请提供此Beta代码帮助,如何解决? [英] Please help with this beta code, How can I fix it?

查看:87
本文介绍了请提供此Beta代码帮助,如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我有代码:

def check_beta_code
    beta_code_array = ['AAAAAAAAAA', 'BBBBBBBBBB', 'CCCCCCCCCC', 'DDDDDDDDDD', 'EEEEEEEEEE']
    beta_code_array.each do |code|
        if :beta_code != code
            errors.add(:beta_code, "Invalid Beta Code")
        end
    end 
end

此代码的问题是,即使有人输入了正确的密码,仍然会产生4个错误,因为另外4个错误。

The problem with this code is that even if someone inputs a correct password, 4 errors are still generated because the other 4 aren't correct.

推荐答案

我假设这段代码在某个地方的模型中。您可以尝试创建一个包含beta代码的模型,并将某些值存储在数据库中。然后只需要查询数据库中的代码并查看是否存在匹配项即可。

I'm assuming this code is in a model somewhere. You can try creating a model that holds the beta codes and store some values in the DB. Then it's just a matter of querying the db for the code and seeing if there's a match:

# model
class BetaCode < ActiveRecord::Base
  # assumes a field named 'code'
end

# add some codes (via migration, console, manually, etc)
['AAAAAAAAAA', 'BBBBBBBBBB', 'CCCCCCCCCC', 'DDDDDDDDDD', 'EEEEEEEEEE'].each do |code|
  BetaCode.create(:code => code )
end

# and check for a match when user submits code (controller)
@code = BetaCode.find_by_code(user_code)

if @code.present?
  # user is approved
else
  # sorry, no luck
end

如果要使用代码段,则始终可以使用 member?方法创建数组。

If you want to use your snippet, you can always use the member? method for an array.

这篇关于请提供此Beta代码帮助,如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆