简体   繁体   English

Rails 将变量添加到另一个变量

[英]Rails add variable to another variable

I am a bit stuck.我有点卡住了。 And I know there should be an easy solution.我知道应该有一个简单的解决方案。 So I have this line of code:所以我有这行代码:

  <% if $current_course.claimedreward1 == false%>

and a variable和一个变量

$number = 1 

I am trying to get the 1 replaced by $number我正在尝试将 1 替换为 $number

I tried我试过

<% if $current_course.claimedreward+$number == false%>

There must be a super simple way to do this.必须有一个超级简单的方法来做到这一点。 I found #{} but it only seems to work for strings.我发现 #{} 但它似乎只适用于字符串。 I am a bit lost.我有点失落。

You could use send to build a method/attribute name programatically:您可以使用send以编程方式构建方法/属性名称:

<% if $current_course.send("claimedreward#{$number}") == false%>

update_attribute("claimedreward#{$number}", true)
update_attributes("claimedreward#{$number}" => true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM