Forum: Ruby on Rails How to pass local variables to render_to_string?

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.
B15edcdfdbecf5723067df3e24e4f6f0?d=identicon&s=25 Adam Block (adamblock)
on 2007-01-20 19:19
I need to use a view to generate some text, using render_to_string. The
form I am using is:

    render_to_string :template => "foo/bar"

The problem is that this form does not accept the :locals attribute.
Therefore any variables I want to be accessible in the view must be
instance variables.

I understand that this is standard view behavior, but whereas the point
of most controllers is to generate instance variables for the view, in
my case that's secondary. And this scheme means I have to think through
ahead of time what will be displayed in the view and make sure that
variable has an @ in front of it.

Is there a more concrete way of passing local variables to this form of
render/render_to_string?

Thanks!

/adam
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-01-20 20:17
(Received via mailing list)
Hi --

On Sat, 20 Jan 2007, Adam Block wrote:

> I understand that this is standard view behavior, but whereas the point
> of most controllers is to generate instance variables for the view, in
> my case that's secondary. And this scheme means I have to think through
> ahead of time what will be displayed in the view and make sure that
> variable has an @ in front of it.
>
> Is there a more concrete way of passing local variables to this form of
> render/render_to_string?

The first thing that comes to mind is putting the values in a hash,
rather than local variables, and passing that hash along in one
instance variable.  I don't know how that would play with the rest of
what you're doing though.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
B15edcdfdbecf5723067df3e24e4f6f0?d=identicon&s=25 Adam Block (adamblock)
on 2007-01-20 20:29
David wrote:
> The first thing that comes to mind is putting the values in a hash,
> rather than local variables, and passing that hash along in one
> instance variable.

Hi David.

Yes, that would be cleaner, or at least more deliberate (since the
packaging of the local controller variables into an instance hash would
all happen in one place instead of the instance variable assignments
floating all over the controller method).

Thanks!

/afb
This topic is locked and can not be replied to.