开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜