开发者

Rails Form with checkboxes

开发者 https://www.devze.com 2023-01-07 03:43 出处:网络
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 wa

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 %>
0

精彩评论

暂无评论...
验证码 换一张
取 消