Nicolas Raoul
Nicolas Raoul

Reputation: 60213

Rails: Form with configurable number of inputs

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] ?

Upvotes: 0

Views: 416

Answers (1)

krichard
krichard

Reputation: 3694

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

Upvotes: 1

Related Questions