This project is archived and is in readonly mode.

#435 ✓invalid
Miguel Vazquez

Fields_for nesting problem

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

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.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages