On Fri, Aug 17, 2012 at 10:52:46AM +0200, Jirka Tomasek wrote:
I don't like the sentence "Don't use it for passing variables to
view. Never." which is actually one of the first things that any
Rails book teaches you.
Yes, that was the main source of my confusion.
I agree that we should try to reduce using
instance variables in controllers where possible to avoid cluttering
controllers with them.
As someone mentioned in another reply here, there are uses where you
really need them -- e.g., before_filters. Of course we should be
careful to not use instance variables where there's no need for them;
sometimes we'll mistakenly use an instance variable just within the
scope of a method, for example.
The blog post you sent points out the reusability of partials so
passing the variables as locals helps avoid requiring use of eg.
@post in all partials instances. Imho it depends on the desired use
of the partial.
We have discussed with Imre the alternative way to pass variables
from controller's action to view. It can be done similar way as with
partials:
...someactioncode...
render :action => :index, :locals => {:post => post}
end
but I have never seen this used anywhere in controller and I assume
this is not the "Rails way".
Yes, this feels pretty weird to me. That whole line should be
unnecessary -- we're overriding the "Rails magic" to explicitly perform
the default behavior, except referencing the variable as a local one. If
anything, this seems _more_ error-prone to me than relying on instance
variables.
-- Matt