How to simplify "render_to_string" in Rails 3?
Asked Answered
L

4

12

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...)

Liquorice answered 20/6, 2011 at 12:14 Comment(0)
K
9

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?

Keldah answered 20/6, 2011 at 12:45 Comment(0)
L
2

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.

Lemmie answered 30/5, 2012 at 11:58 Comment(0)
F
0

You can always add your own method to ApplicationController and make it available as a helper method if you need to use it in views.

Frink answered 5/11, 2013 at 19:0 Comment(0)
U
-1

I would use a Mailer for that. And then do.

MyMailer.my_method.body.encoded. You can use helpers this way too.

Urban answered 12/9, 2012 at 1:24 Comment(1)
It seems to be very hackety.Frink

© 2022 - 2024 — McMap. All rights reserved.