I would like to add dynamically new input (select) field, this is ok and done by JQuery, but when passing to the controller create
it keeps only the first entry.
This is my new.html.erb
view
<%= 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 %>
The params that I would like to recieve in my controller is something like this:
params[:user_id]
=> OK
params[:competitor_id]
=> an array of all IDs that the user has inserted (can be only 1 or +).
For example, if the user "clicks" three times the add buttons, there are 4 competitor_id
and the params[:competitor_id]
should be something like:
params[:competitor_id] = [71,32,65,234]
in the order of selects.
How can I convert the various :competitor_id
selects in one array?