请提供此Beta代码帮助,如何解决?

编程入门 行业动态 更新时间:2024-10-23 01:51:57
本文介绍了请提供此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代码帮助,如何解决?

本文发布于:2023-11-11 07:02:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577710.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   代码   Beta

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!