Question

I need to make a form for a team which consists of 3 members.
I have my model setup as:

field :name1,:name2,:name3   
field :email1,:email2,:email3  

i don't want to write 3 forms in views. How can I dynamically create the symbols which simple_form requires? Some thing like:

<% for i in 0..2 %>
  <%=f.input :name+i %>
  <%=f.input :email+i %>
<% end %>
Was it helpful?

Solution

You are missing the intern method on strings.

Do it like this:

<% for i in 0..2 %>
  <%=f.input "name#{i}".intern %>
  <%=f.input "email#{i}".intern %>
<% end %>

You can also use the to_sym method.

OTHER TIPS

Something like:

<% for i in 0..2 %>
  <%=f.input "name#{i}".to_sym %>
  <%=f.input "email#{i}".to_sym %>
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top