简体   繁体   中英

Generate array of textbox in Ruby on Rails

I am using the below ruby code

<%= form_for :post, url:"/index" do |f|%>
    <%= f.text_field(:name) %>
<% end %>

This generates a form with textbox as below

<input type="text" name="post[name]" id="post_name">

Is there any way to generate fields with array format such as:

<input type="text" name="post[name][]" id="post_name_1">
<input type="text" name="post[name][]" id="post_name_2">
<input type="text" name="post[name][]" id="post_name_3">
<input type="text" name="post[name][]" id="post_name_4">
:
:
<input type="text" name="post[name][]" id="post_name_n">

You can override the name like this

<%= f.text_field(:field_name, :name => "post[name][]") %>

and then do it like this:

<%(1..n).each do |i|%>
<%= f.text_field(:name, :name => "post[name][]", :id => "post_name_#{i}") %>
<%end%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM