Partial views are super common in just about all web frameworks; Rails is no different. I came to Rails from another web framework so I already knew the power of partial views. Naturally I’ve been using them everywhere I can to simplify and separate views. About the only thing I dislike about Rails partials is the double syntax for the locals variables. There is one syntax for a single model with the same name as the partial and another syntax for arbitrary named local variables. I tend to prefer consistency, so I mostly use the locals syntax.
I tried everything I could think of to access the form’s ID via the local variable. I even searched the instance variables (using #instance_variables) of all the classes I could think of. I even looked back at partials I had created in the past. As far as I could tell, I was trying to access the variable correctly.
It turns out the locals are accessed via instance accessor methods. This whole time I have been naming my controller’s instance variables the same as my partial’s local variables. This is why they always worked before. Only when I finally made a local variable that wasn’t also an instance variable did I discover the difference.
Drop the @ symbol in front of your variable while inside a partial view. Use them as instance methods and not as instance variables. I know my Google-fu couldn’t find the answer. Hopefully yours will find this article before you pull all of your hair out.
Special thanks to my co-worker Daniel Zajic for pairing with me to solve the problem. He found the solution as soon as I told it to him. Clearly he had read the documentation I should have read earlier.