Forum: Ruby on Rails Locals in render partials

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
453493fcee22adbf3c46ac793a346ef5?d=identicon&s=25 Nike Mike (nike)
on 2009-04-03 06:55
In render partials can we pass arrays like this


a=[1,2,3]
b=[5,6,7]
c << a << b

render :partial=>'',:locals=>{:c=>c}
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-04-03 14:14
(Received via mailing list)
Nike Mike wrote:
> In render partials can we pass arrays like this
>
>
> a=[1,2,3]
> b=[5,6,7]
> c << a << b
>
> render :partial=>'',:locals=>{:c=>c}

Yes. Write a functional test on the behavior of the page with the
partial, and
you should be able to detect the effects of :c inside there.

BTW, I always name variables passed in :locals like :@c, so they appear
as @c
inside. This makes them easier to test for nil, to mean "unassigned".

(Do the latest versions of Rails and Ruby support that?)
Ea52242ae25d195415cd9ef31db72aeb?d=identicon&s=25 Felix (Guest)
on 2009-04-03 14:35
(Received via mailing list)
I tried your suggestion of passing locals as :@var on ruby 1.8.6 and
rails 2.2.2 and it doesn't seem to work
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-04-03 15:56
(Received via mailing list)
Felix wrote:

> I tried your suggestion of passing locals as :@var on ruby 1.8.6 and
> rails 2.2.2 and it doesn't seem to work

Tx...

Not to hijack the thread (I'm unsure if Nike Mike's question feels
answered
yet), but we are having a major problem with...

  - too many tests
  - too many render :partial and render :inline calls
  - too many @ variables magically passed without :locals

Apparently we reach a threshold where the magic stops working, and our
most
important @ variables disappear. This leads to a cascade of incorrect
test
failures on missing nils. (The infamous "whiny nil" takes over.)

So if I fix that using :locals =>{ :@q => @q } in every render call,
then I
impair our project for upgrading to 2.2.2...

Elaborate sigh...

The problem with :locals => { :q => @q } is you can't say 'if q' inside
the
partial to detect if the local is set. You can say 'if @q', because you
rely on
Ruby to provide a nil for decorated variables that don't exist yet...

I seem to recall you can't do 'if defined? q', either. Maybe.

This means the final recourse is too ugly for Rails: :locals =>{ :things
=>{ :q
=> @q } }. Now the partial can use 'if things[:q]'...

--
   Phlip
This topic is locked and can not be replied to.