Re: Help with nested models and fields_for

Mark Reginald James
Sat, 20 Dec 2008 13:22:06 -0000

Marnen Laibow-Koser wrote:

>   <input id="recipe_ingredient_lines__ingredient_name"
name="recipe
> [ingredient_lines][][ingredient][name]" size="30"
type="text" />
>
> So far so good.  But here''s the problem: *submissions
don''t work quite
> properly*.  When I fill out the form and submit it, I get params
> entries like
> "recipe"=>{"name"=>"Recipe",
"ingredient_lines"=>[{"quantity"=>"1",
> "unit"=>"cup", "ingredient"=>{}}],
"instructions"=>"Preheat oven to
> 350°..."}. Note that ingredient_lines[0].ingredient.name is missing,
> even if I enter text in the corresponding form field -- in other
> words, what I should be seeing is something like "recipe"=>
> {"name"=>"Recipe",
"ingredient_lines"=>[{"quantity"=>"1",
> "unit"=>"cup",
"ingredient"=>{"name" => "flour"}}],
> "instructions"=>"Preheat oven to 350°..."}.

I believe this is a limitation of Rails'' parameter parsing.
It can''t handle more that one level below an array parameter.

A solution is to create an ingredient_name accessor in
IngredientLine, and arrange to have this rendered and posted.