Render partial to String in view helper
Asked Answered
B

2

8

How can I render a partial to a String in Ruby on Rails 4.2, since render_to_string is deprecated?

Something like:

 rendered_string = render_to_string partial: 'forgotten_orders/soonest_manufacturing_date', locals: { forgotten_order: forgotten_order, soonest_manufacturing_date: soonest_manufacturing_date }
Baggett answered 6/5, 2015 at 14:59 Comment(0)
F
13

You can just use render. My demo:

In my view:

<% foo = render 'foo_thing', bar:"formal bar" %>
<%= foo %>

In _foo_thing.html.erb:

<%= "This bar is a #{bar}" %>

And on my screen:

 This bar is a formal bar 
Fab answered 6/5, 2015 at 15:11 Comment(1)
Yep I guess #render does work in a helper, but this answer doesn't show a helper method!Giacopo
S
0

Well, render_to_string actually is not deprecated, it still exists. The implementation however has changed according to the render_to_string doc. I haven't really used it in a while, but I'd expect it to still work for you.

Stubbed answered 6/5, 2015 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.