以循环、嵌套形式生成属性名称
我使用以下代码以 Rails(3.06) 形式生成一组字段:
<% (1..5).each do |index| %>
<%= f.text_field "s"+index.to_s + "_incidence", :placeholder => "incidence", :value => number_to_percentage("s"+index.to_s + "_incidence", :precision => 0 ) %>
<% end %>
循环根据项目中的请求数量创建字段 - s0_incidence、s1_incidence、s2_incidence 等。因此,我没有将每行重复 10 次,而是使用循环来创建字段。
表单提交正确,但是对于现有记录,我想使用 number_to_percentage 公式填充 :value 。 number_to_percentage 拒绝识别属性名称,因此单元格的内容看起来像“s0_incidence”而不是##%。
我做错了什么?我是否需要将 *"s"+index.to_s + "_incidence"* 转换为符号才能将其识别为 :s0_incidence?
I am using the following code to generate a set of fields in a rails(3.06) form:
<% (1..5).each do |index| %>
<%= f.text_field "s"+index.to_s + "_incidence", :placeholder => "incidence", :value => number_to_percentage("s"+index.to_s + "_incidence", :precision => 0 ) %>
<% end %>
The loop creates fields - s0_incidence, s1_incidence, s2_incidence, etc. depending on the number of requests in the project. So instead of repeating each line 10 times, I use the loop to create the fields.
The form submits correctly, however for existing records, I would like to populate the :value using number_to_percentage formula. number_to_percentage refuses to recognize attribute name, so the contents of the cell look like "s0_incidence" instead of ##%.
What am I doing wrong? Do I need to convert *"s"+index.to_s + "_incidence"* to symbol to have it recognized as :s0_incidence?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
number_to_percentage
想要一个数字。它对您的表单或模型一无所知,因此无法采用参数名称。如果我正确理解您的请求模型,我认为您需要这样做
number_to_percentage
wants a number. It knows nothing of your form or model so it can't take a parameter name.If I'm understanding your requests model correctly I think you need to do
想通了:
Figured it out: