开发者

Rails: Form with configurable number of inputs

开发者 https://www.devze.com 2023-03-18 02:30 出处:网络
I wrote a form that generates a text input for each property. The list of properties is configurable by the customer.

I wrote a form that generates a text input for each property.

The list of properties is configurable by the customer.

<% properties = ["refractivity_at_2kHz", "refractivity_at_5kHz"] %>

<% properties.each do |property| %>
  <div class="property">
    <%= f.label property %>
    <%= f.text_field property %>
  </div>
<% end %>

It fails with the error undefined method refractivity_at_2kHz.

What is the usual solution for this problem?

Should I add an array to 开发者_开发技巧my model, and use f.text_field myarray[property] ?


Is it a form_for(@model)?

Because then f.text_field(property) looks for that method/property on @model.

May be you want to change f.text_field(property) into text_field_tag(property)[1]

cheers

[1] http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag

0

精彩评论

暂无评论...
验证码 换一张
取 消