Rails Form with checkboxes
I have two models, Leads and courses, Leads HABTM courses.
I want to list all courses in leads/new as checkboxes, at the moment I have them as a multiselect b开发者_高级运维ox, but I dont like the way it works.
This is what I have at the moment
<%= f.collection_select :course_ids, Course.find(:all, :order => 'course_type'),
:id, :course_name, {}, :multiple => true %>
How would I make it so it lists all of the courses?
This Railscast provides one possible solution:
<% for course in Course.all(:order => 'course_type') %>
<div>
<%= check_box_tag "lead[course_ids][]", course.id, @lead.courses.include?(course) %>
<%= course.name %>
</div>
<% end %>
I believe this is what you're looking for
# controller
@courses = Course.find(:all, :order => 'course_type')
# view
<% @courses.each do |course| %>
<%= f.check_box :course_id %><%= course.name %>
<% end %>
精彩评论