4
votes

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?

2

2 Answers

0
votes

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.

0
votes

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)