开发者

How do I make checkboxes for a has_many :through association using the form builder?

开发者 https://www.devze.com 2023-02-17 08:32 出处:网络
Want to do a checkboxes for has_many :through. Railscast in 2007 recommends this:check_box_tag \"product[category_ids][]\",开发者_如何学运维 category.id, @product.categories.include?(category). Is thi

Want to do a checkboxes for has_many :through. Railscast in 2007 recommends this: check_box_tag "product[category_ids][]",开发者_如何学运维 category.id, @product.categories.include?(category). Is this still relevant or is there a more natural way using form_for to do this in rails 3?


My recommendation is to check out Justin French's Formtastic gem: https://github.com/justinfrench/formtastic

It makes working with forms in rails really easy and intuitive.

Your form might look like this:

<%= semantic_form_for @product do |f| %>
  <%= f.inputs do |f| %>
    <%= f.input :name %>
    <%= f.input :categories, :as => :check_boxes, :collection => Categories.all %>
  <% end %>
  <%= f.buttons %>
<% end %>

Much simpler then looping through and using the check box tag.

Once you go formtastic you never go back.

0

精彩评论

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

关注公众号