No, this is not a real Sinatra error :).
This is the personal homepage of Elad Meidar, a web developer and an entrepreneur specializing in Ruby on Rails. I hang around in Israel, and i am currently having the best time of my life over at Fiverr.com
I am a proud member of RailsBridge, Helping new Rails developers get into our world and also contributed a few Patches to the Ruby on Rails core.
Surprisingly, i didn’t find anyway to do it. ( Another patch is waiting to the next BugMash festival).
Trying to find some ways to access it, i turned to the rails code (always a good idea to have a frozen rails in your
vendor/) and tracked down the method that is responsible for rendering the
fields_for nested attributes in the
ActionView::Helpers::FormBuilder class, surprisingly, again, it was named
“Way to go, Sherlock”.
A short read indicated that the current child index is determined by either being specified by the developer (
:child_index => something in your
fields_for options) or by an automatic value (0 and up) assigned by an auto incremented instance variable named
My first attempt was to access it with the doomsday device,
#send and snatch it, but the inevitable failure arrived when the infamous @lifo pointed me to the fact that when accessing the
nested_child_index method, i am actually incrementing the index value.
As a last resort i turned to my (seriously neglected) metaprogramming skills to try and add an unobtrusive getter method for the current child index in a
fields_for nested attribute block.
fields_for_child_index.rb in my
Kind of messy i know, and i am sure that there’s a better way to have store the current association name without going into
alias_method_chain, but this is working and will work until i’ll create the core patch for that.
Of course, in the meantime, i am open to improvement suggestions, anyway, this is how you use it:
#current_child_index will return the current child index for every element in the
unless you use it EXACTLY as i said, i can’t assure it’s working.. it’s just a simple bypass until the patch will be ready. use at your own risk.
You're seeing this error because I think it is funny.