How to pass local variables to render_to_string?

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

Hi –

On Sat, 20 Jan 2007, Adam B. 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 (Ruby for Rails)
(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)

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