Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Fields_for nesting problem

#435

I have a business object that may contain other objects of the same type recursively. I did a partial that could include itself, It is attached (The code is in spanish, sorry!). The problem was that some nested properties where not filled out.

For example, for the paramters as follows:

[["authenticity_token", "676f0eceda432add232e14d03adf74b03f03ea25"],

["plano[nombre]", "a"],

["plano[pieza_attrs][][posicion]", "1"],

["plano[plano_attrs][][nombre]", "c"],

["plano[plano_attrs][][pieza_attrs][][posicion]", "3"],

["commit", "Crear"]]

Resulted in:

{"commit"=>"Crear", "authenticity_token"=>"676f0eceda432add232e14d03adf74b03f03ea25", "plano"=>{"pieza_attrs"=>[{"posicion"=>"1"}], "plano_attrs"=>[{"pieza_attrs"=>[], "nombre"=>"c"}], "nombre"=>"a"}}

when it should have been

{"commit"=>"Crear", "authenticity_token"=>"676f0eceda432add232e14d03adf74b03f03ea25", "plano"=>{"pieza_attrs"=>[{"posicion"=>"1"}], "plano_attrs"=>[{"pieza_attrs"=>[{"posicion" => "3"}], "nombre"=>"c"}], "nombre"=>"a"}}

After long hours diving into the Rails code I found that a simple change in

actionpack-2.0.2/lib/action_controller/request.rb

did the trick, def bind(key, value)

if top.is_a? Array

if key

if top[-1].is_a?(Hash) && ! top[-1].key?(key)

top[-1][key] = value

  • value = top[-1][key] # Added by Miguel Vazquez

else

top << {key => value}.with_indifferent_access

push top.last

end

else

top << value

end

elsif top.is_a? Hash

key = CGI.unescape(key)

parent << (@top = {}) if top.key?(key) && parent.is_a?(Array)

return top[key]

else

raise ArgumentError, "Don't know what to do: top is #{top.inspect}"

end

return value

end

Note that it also includes a change I found in http://dev.rubyonrails.org/ticke...

I really hope this ticket helps.

PS: I'd love to know who come that fixed the problem.

Reported by Miguel Vazquez · June 17th, 2008 @ 03:52 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Miguel Vazquez
    Miguel Vazquez

    Sorry for the crappy format, It's late and I'm really tired :(.

    June 17th, 2008 @ 03:53 AM

  2. Miguel Vazquez
    Miguel Vazquez
    • No changes were found…

    June 17th, 2008 @ 12:26 PM

  3. Miguel Vazquez
    Miguel Vazquez

    This was fixed already in the latest version

    June 17th, 2008 @ 12:27 PM

  4. Pratik
    Pratik
    • State changed from new to invalid
    • Tag set to 2.0-stable, actionpack, bug, patch

    July 14th, 2008 @ 04:00 AM