我想动态添加新的输入(选择)字段,这可以通过 JQuery 完成,但是当传递给控制器时,create
它只保留第一个条目。
这是我的new.html.erb
看法
<%= simple_form_for @comp, :url => url_for(:controller => 'competitors', :action => 'create'), :method => 'post' do |f| %>
<div id="form_inputs">
<%= f.error_notification %>
<br />
<%= f.collection_select :user_id, User.order("LOWER(name) ASC"), :id, :name, :prompt => "Select an user", required: true %>
<span class="label label-important"><%= f.error :user_id %></span>
<br />
<%= f.collection_select :competitor_id, Company.order("LOWER(name) ASC"), :fb_id, :name, :prompt => "Select a competitor"%>
<span class="label label-important"><%= f.error :competitor_id %></span>
<br />
</div>
<div class="add">Add another competitor <%= image_tag "add.png" %></div>
<br />
<%= f.button :submit, "Create new competitor!" %>
<script type="text/javascript">
$(".add").on('click', function(event){
event.preventDefault();
$("#form_inputs").append("<%= escape_javascript(f.collection_select :competitor_id, Company.order('LOWER(name) ASC'), :fb_id, :name, :prompt => 'Select a competitor') %> <span class='label label-important'><%= escape_javascript(f.error :competitor_id) %></span><br /> ");
$('select').selectpicker();
});
</script>
<% end %>
我想在我的控制器中收到的参数是这样的:
params[:user_id]
=> OK
params[:competitor_id]
=> 用户插入的所有 ID 的数组(只能是 1 或 +)。
例如,如果用户“点击”了 3 次添加按钮,则有 4 次competitor_id
,params[:competitor_id]
应该类似于:
params[:competitor_id] = [71,32,65,234]
按选择的顺序。
如何:competitor_id
在一个数组中转换各种选择?