You are hereProblems with protected controller methods
Problems with protected controller methods
I struggled with this for awhile, maybe this post will save someone else the trouble?
By default, methods in your Rails controller can be accessed from the URL of your application. If you have a controller method named "car" and a method named "start", then using the URL car/start will execute the "start" method.
But, there are some cases where you might want a method in the controller, but not have it accessible. In this case, you can make the method "protected", and only methods within the same controller will have access to it.
I was happy and successful understanding these details until I tried to use a protected method inside an AJAX-style page update block. In this case, the protected method couldn't be found. I couldn't understand why the method worked in most cases, but not this one. I would get an error like:
NameError (undefined local variable or method `top_level_nodes' for #<#:0x46e7ec8>):
Then I realized that putting the call to my protected method inside a block like this:
render :update do |page|
protected_method
end
... is actually calling the method from a different class, and that's why the method is not found.
Once I realized this, I was able to move the call outside of the render block and I was good. I'm not sure if there is any other way to handle cases like this. I also noticed that the call would fail similarly inside a "for x in y" block.