Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify "render_to_string" in Rails 3?

To render a partial in Rails 3 I do:

render 'my_partial', :my_object => my_value

To get partial's result as string I do:

render_to_string(:partial => 'my_partial', :layout => false, 
                 :locals => {:my_object => my_value})

Is this possible to write the last one shorter ? (e.g. to omit :partial or :locals like I do with render) (I tried, but get errors...)

like image 315
Misha Moroshko Avatar asked Jun 20 '11 12:06

Misha Moroshko


2 Answers

Doesn't seem like it could get any shorter, Ref. If you feel it's too long you could hide it in a helper method, and just pass the various params?

like image 175
Michael De Silva Avatar answered Nov 15 '22 16:11

Michael De Silva


Sometimes you could do something like

 render_to_body "path_to_file"

and it skips layout but still you have to use instance variables inside your partial.

like image 36
prikha Avatar answered Nov 15 '22 15:11

prikha