简体   繁体   中英

How to create Formtastic “partials”

Basically, I'm trying to create a dynamic group of check boxes that are keyed off a category select in the same form. For example, a user would choose a category from the select, and then a list of corresponding subcategory checkboxes would appear.

I've done this before with only selects, but those are much easier because you only have to supply a generic set of options. Check boxes (especially with formtastic) have a lot of corresponding markup that I'd rather not generate myself.

My question, then, is how to get formtastic to create only the proper check boxes but still have their name and id fields contain all the correctly nested information. I want it to act exactly like the following, but only output the last line for me to send over ajax.

= semantic_form_for @user do |u|
  = u.inputs :name, :age
  = semantic_fields_for :job do |f|
    = f.input :category, :as => :select, :collection => Category.all
    = f.input :subcategory, :as => :check_boxes, :collection => # This is what needs to be dynamic

I've tried just using the last line wrapped in a generic semantic_fields_for , but the field names are no longer correct.

How would you do it?

You'd need to use a helper and define it as a method. eg:

def form_boxes(f)
  f.input :subcategory, :as => :check_boxes, :collection => stuff_goes_here
end

and then call in your view:

= form_boxes(f)

I would try this:

= semantic_fields_for @user do |u|
  = semantic_fields_for :job do |f|
    = f.input :subcategory, :as => :check_boxes, :collection => some_thing

I'm pretty sure that should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM