Reputation: 683
Currently I have a Category and Post model, joined by a HABTM relationship.
Posts belong to multiple categories and have many attributes.
Categories just have a Name Property.
How do I create a multi-select form in my Posts _form.html.erb so I can select which categories I want the post to be assigned to?
Upvotes: 0
Views: 36
Reputation: 1034
<%= form_for @post do |f| %>
<div>
<%= f.label :category_ids, "Categories" %><br />
<%= f.collection_select :category_ids, Category.order(:name), :id, :name, {}, {multiple: true} %>
</div>
<div>
<%= f.submit 'Submit' %>
</div>
<% end %>
Upvotes: 1
Reputation: 1318
Try using select
and collection
. You may have to change the collection, not sure exactly what Category options you want to be able to choose from. Something like this:
<%= f.input :post, as: :select, collection: Category.posts, include_blank:false %>
Or checkout out the collection_select
form helper method
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
Upvotes: 0