2
想做一個has_many:through的複選框。 Railscast在2007年推薦:check_box_tag「product [category_ids] []」,category.id,@ product.categories.include?(category)。這仍然是相關的還是有更自然的方式使用form_for在軌道3中做到這一點?如何使has_many的複選框:通過使用表單生成器的關聯?
想做一個has_many:through的複選框。 Railscast在2007年推薦:check_box_tag「product [category_ids] []」,category.id,@ product.categories.include?(category)。這仍然是相關的還是有更自然的方式使用form_for在軌道3中做到這一點?如何使has_many的複選框:通過使用表單生成器的關聯?
我的建議是看看賈斯汀法國的Formtastic寶石:https://github.com/justinfrench/formtastic
它與在軌非常容易和直觀的形式工作。
你的形式可能是這樣的:
<%= 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 %>
簡單多了,然後通過循環和使用複選框標記。
一旦你走向formtastic你永遠不會回去。