以循环、嵌套形式生成属性名称

发布于 2024-11-02 06:34:49 字数 657 浏览 1 评论 0原文

我使用以下代码以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女猫性小仙女 2024-11-09 06:34:49

number_to_percentage 想要一个数字。它对您的表单或模型一无所知,因此无法采用参数名称。

如果我正确理解您的请求模型,我认为您需要这样做

<% @project.requests.each_with_index do |request, index| %>
  <%= f.text_field "s#{index}_incidence", :placeholder => "incidence", :value => number_to_percentage(request.incidence, :precision => 0) %>
<% end %>

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

<% @project.requests.each_with_index do |request, index| %>
  <%= f.text_field "s#{index}_incidence", :placeholder => "incidence", :value => number_to_percentage(request.incidence, :precision => 0) %>
<% end %>
呆° 2024-11-09 06:34:49

想通了:

<% (1..5).each do |index| %>  
  <%= f.text_field "s"+index.to_s + "_incidence", :value => number_to_percentage(f.object["s"+index.to_s + "_incidence"], :precision => 0), :placeholder => "incidence" %>                                                       
<% end %>

Figured it out:

<% (1..5).each do |index| %>  
  <%= f.text_field "s"+index.to_s + "_incidence", :value => number_to_percentage(f.object["s"+index.to_s + "_incidence"], :precision => 0), :placeholder => "incidence" %>                                                       
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文