views:

1199

answers:

4

This problem has been killing me. I played around with Ryan Bates complex forms, but I can't quite figure out my problem. I have this schema:


Location has_many :targets
Target has_many :target_classifications
All locations are shown on the page. A user may create a target for any location dynamically through jscript, which then adds a table row under the location 3 selects (that contain available classifications to the target) and a target value. Any number of targets can be created for any location before clicking save. I'm using rjs to render a target_partial, which has this code: I'm using fields_for in this way:


for each select. When sumbmitted, I get this hash:
"new_targets"=>
  {"7"=>[{"id"=>"13"}, {"id"=>"15"}, {"value"=>"67", "id"=>""}],
   "4"=>
    [{"id"=>"12"},
     {"id"=>"15"},
     {"value"=>"23", "id"=>""},
     {"id"=>"11"},
     {"id"=>"16"},
     {"value"=>"67", "id"=>""}]},

So, it separates each target by location ("7" and "4" in this case), but doesn't separate each target. What I want is this:

"new_targets"=>
  {"7"=>[
          {"target"=>[{"id"=>"13"}, {"id"=>"15"}, {"tonnes"=>"67"}]}
        ],
   "4"=>[
          {"target"=>[{"id"=>"12"},{"id"=>"15"},{"tonnes"=>"23"]},
          {"target"=>[{"id"=>"11"},{"id"=>"16"},{"tonnes"=>"67"]}
        ]
  }

so I can iterate through each target for each location. I can't seem to add in a new [target] brace in my field_for method (it blows up), but that's kind of what I want to do. Any thoughts?

A: 

Can you post your form code?

Matt Darby
the above code produces this select<select id="new_targets_4__id" class="selectbox" name="new_targets[4][][id]"> To simplify things, i don't care about location really, i can just pass the id as another param. I just want an array target hashes, pointing to the target params
brad
A: 

This sort of issue is discussed here:

http://wonderfullyflawed.com/2009/02/17/rails-forms-microformat/

But it seems to be a rails 2.3 solution (not an option for me). Basically, I want a form like they've posted on the site:

<input name="creator[widget_attributes][0][id]" />
<input name="creator[widget_attributes][0][name]" />
<input name="creator[widget_attributes][0][price]" />

<input name="creator[widget_attributes][1][id]" />
<input name="creator[widget_attributes][1][name]" />
<input name="creator[widget_attributes][1][price]" />

Just some way to create a unique identifier for each target (or in this case, widget attribute) I want to add. Kind of tough given that a new target is added with jscript. I feel like there should be some way for rails to automatically do this for me

brad
+1  A: 

I don't know if you are doing this, but you need to specify a string instead of the object when using fields_for. I can't see the codes you have in your partial, so I may be way off. Anyway, the way I do it is in a helper:

def fields_for_target(target, &block)
  prefix = target.new_record? ? 'new' : 'existing'
  fields_for("location[#{prefix}_target_attributes][]", target, &block)
end
Jaryl
@brad: I'm not sure, but I think these "[]" in fields_for will do what you want
klew
A: 

Easy. Look up accepts_nested_attributes_for. :)

As I mentioned rails 2.3 is not an option for me at the moment. So that doesn't work
brad