Reputation: 382
So I have Users and Interests and they are associated with a has_and_belongs_to_many relation through an interests_users table. What I want to do is to let users select 5 interests when they create their account through checkboxes, but I don't now how to submit the same param 5 times in the same form and I am currently submiting it with checkbox, but Interest is not a boolean. The code right now is like this:
<div class = "field">
<%= f.label :interests %><br>
<% all_interests = Interest.all.map{ |f| [f.name, f.id] } %>
<% all_interests.each do |interest| %>
<p> <%= interest[0] %> <%= f.check_box :interests %> <p>
<% end %>
</div>
Upvotes: 0
Views: 62
Reputation: 4156
In view use the flowing
<% for interest in Interest.all %>
<%= check_box_tag "user[interest_ids][]", interest.id, @user.interests.include?(interest) %>
<%= interest.name %>
<% end %>
In controller accept params as following
params.require(:user).permit( { interest_ids:[] })
Upvotes: 2
Reputation: 118289
You should try this code :
<div class = "field">
<%= f.label :interests %><br>
<% Interest.all.each do |interest| %>
<p> <%= interest.name %> <%= f.check_box :interests, { :multiple => true }, interest.id, nil %> </p>
<% end %>
</div>
Upvotes: 0