Reputation: 1175
Here is my codes:
<%=form_tag('/user_group_follows/follow',:id=>'follow_select_form',:remote=>true,:method=>:get) do %>
<p>You want to add this user to?</p>
<%=hidden_field_tag 'user_id',@user.id%>
<%@user.user_groups.each do |ug|%>
<%=check_box_tag 'user_group_id',ug.id,false,{:id=>'user_group_id_'+ug.id.to_s}%><%=ug.name%><br/>
<%end%>
<%end%>
//using jquery-ui, so there is no submit button....
I wanna the user to make a multiple choice to decide which groups that he/she would like to add into the following list.
So I made several checkboxes with the same name as 'user_group_id' and different ids. I could successfully get the params throught params[:user_group_id], if the user only checked one box. But if he truly checked several of them, how to get this value set in controller? In this circumstance, params[:user_group_id] could only get one of them. And I quite believe codes like: params[:user_group_id_+XXX.id] is not going to work....
Upvotes: 3
Views: 5890
Reputation: 1277
if you go in this way <%=check_box_tag 'user_group_id[]'%> it's returning array of selected ids,
<%=check_box_tag 'user_group_id',ug.id, params[:user_group_id].try(:include?,ug.id.to_s),{:id=>'user_group_id_'+ug.id.to_s}%>
Upvotes: 0
Reputation: 33954
If you name them with id's like user_group_id['+ug.id+']
, I think you should get params like params[:user_group_id]
which should contain an array of all the id's of groups that were checked.
Something like this, not sure exactly, but basically you want to name your fields such that they are grouped into an array naturally, by virtue of how they are named:
<%=check_box_tag 'user_group_id['+ug.id']',ug.id,false,{:id=>'user_group_id_'+ug.id.to_s}%><%=ug.name%>
So, params[:user_group_id].first
would contain the id of the first checkbox that was selected.
Upvotes: 5