0

I have a form that I am trying to build to edit multiple records. It's complicated, doesn't map straight to the database, and there can be any number of records. I have the code written so all of the data is passed to the view as a hash. Like this:

 @formdata = {"datafield_1"=>"value_1", "datafield_2"=>"value_2"}

What I want to do is to create something like:

 f.textfield :datafield_1
 f.textfield :datafield_2
 f.textfield :datafield_3
 etc. etc. etc.

But I don't know how to pass the index of my for loop into the variable name. In short, how do I do :datafield_i where i is my index?

1 Answer 1

0
<% %w(1 2 3).each do |i| %>
  <%= f.textfield(:"datafield_#{i}") -%>
<% end %>

or

<% @formdata.keys.each do |datafield| %>
  <%= f.textfield(datafield.to_sym) -%>
<% end %>
3
  • Take that back, this <%= concat(f.input :"start_date_#{i}"), input_html: { value: @ratesform["start_date_1"]} %> says it is missing a ) at the comma before input_html Oct 5, 2012 at 15:18
  • Ok, I figured it out. I can use concat to make a string and then the to_sym to make the variable. Thanks a bunch. Oct 5, 2012 at 15:29
  • <% concat('foo') %> and <%= 'foo' -%> are the same.
    – oldergod
    Oct 5, 2012 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.