views:

40

answers:

2

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 box, 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?

A: 

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 %>
j.
+3  A: 

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 %>
John Topley
Thanks a lot that worked perfectly, I found the railscast after I posted here.
Josh Crowder