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 }
fkoessler
  • 6,932
  • 11
  • 60
  • 92

2 Answers2

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 
steve klein
  • 2,566
  • 1
  • 14
  • 27
  • 2
    Yep I guess `#render` does work in a helper, but this answer doesn't show a helper method! – Chloe Oct 09 '17 at 17:42
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.

Ninigi
  • 1,311
  • 12
  • 19