Skip to content

Instantly share code, notes, and snippets.

@acorcutt
Created July 22, 2011 16:41
Show Gist options
  • Save acorcutt/1099813 to your computer and use it in GitHub Desktop.
Save acorcutt/1099813 to your computer and use it in GitHub Desktop.
Post array of text_field's in rails as array
<%-# items is an array on object e.g. object.items = [1,2,3], we want to display as list of fields, and post back as an array - note the backwards ][ which makes is do this! -%>
<% @object.items.each do |i| %>
<%=f.text_field "items][",:value=>i,:id=>"item-#{SecureRandom.uuid}" %>
<% end %>
@ruanltbg
Copy link

Liked.

@ChaosWars
Copy link

Nice. The only issue is if you also have a label for each corresponding text field, clicking on any label selects the first text field.

<%= f.label "item][", "Item #{i + 1}" %>
<%= f.text_field "item][", value: i %>

Do you know a way to correct this? It may be impossible due to the naming conventions I suppose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment