Avraam Mavridis
Avraam Mavridis

Reputation: 8920

render_to_string returns empty string when called from controller

I am using rails 5.1, I have a controller, when I try to render to a string I am getting an empty string

def pdf_string
    path = Rails.root.join("/app/views/menu/recipe_card.pdf.erb").to_s;

   render_to_string(
      :file => path,
      locals: get_pdf_locals
    )
end

But if I do

ActionController::Base.new.render_to_string(
  :file => path,
  locals: get_pdf_locals
)

I am getting the view rendered. The problem is that using ActionController::Base.new inside my controller seems kinda odd, and the view is not having access to helper methods. Any idea why the render_to_string of my controller is returning emtpy string while the ActionController::Base.new.render_to_string is rendering correctly?

Upvotes: 4

Views: 1110

Answers (1)

Avraam Mavridis
Avraam Mavridis

Reputation: 8920

The problem was that my controller was extending ActionController::API instead of ActionController::Base, unfortunately the render_to_string was returning empty string, it would be easier to debug if it was failing hard.

Upvotes: 4

Related Questions