需要如何在字段名称有效转换字符串值:转换字符串导轨
实施例:
<%= “price.list _” + current_user.price.to_s%>
所以
price.list_1
然后是我真实的字段名称。这个name.this字段将使用它在我看来做更多的操作。
需要如何在字段名称有效转换字符串值:转换字符串导轨
实施例:
<%= “price.list _” + current_user.price.to_s%>
所以
price.list_1
然后是我真实的字段名称。这个name.this字段将使用它在我看来做更多的操作。
我想我明白你的问题。您将需要使用发送功能
<%= price.send("list_#{current_user.price}".to_sym) %>
这应该工作,但你也可以做
<%= "price.list_#{current_user.price.to_s}" %>
OR
<p>
price.list_<%= current_user.price.to_s %>
</p>
更新:我误解了这个问题。这将需要一些Javascript或AJAX,这取决于您的具体应用。
JS:在轨道上
:onchange => 'update(this.value)'
function update(new_value) {
var update_me = document.getElementById('update_this_field');
update_me.value = new_value;
}
AJAX
:onchange => remote_function(:url => {:action => 'update'}, :with => 'Form.element.serialize(this)'), :name => 'foo'
def update
bar = params[:foo]
render :update do |page|
page.replace_html 'update_this_field', "price.list_" + bar
end
end
此外,通常,人们分配** @ current_user **,因此请确保您得到了正确的结果。 – 2011-03-22 20:49:18
感谢@凯尔梅西,但我仍然打印“price.list_1”,而不是在该字段的值 – Macoto 2011-03-22 20:57:46
对不起,我只需要打印多个列动态生成每个列名称。 price.list_1是列的名称,但根据注册用户进行更改,以便在此情况下,“current_user.price”的值为1,如果连接“price.list_”+ current_user.price.to_s,则我生成价格。 list_1是正确的,但它生成的字符串名称,而不是价格price.list_1内的价值将是56.02例如,希望我已经解释和道歉 – Macoto 2011-03-22 21:13:54
不错而且非常棒! TNKS! – Macoto 2011-03-23 05:18:42
如果你的问题解决了,你能接受答案吗?谢谢 – Shiv 2011-03-23 16:43:19