4

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?

4
  • That is most certainly very odd. Did you try using pry-byebug to set a breakpoint and step into the method to see whether it maybe was overriden somewhere? Commented Jan 19, 2019 at 9:35
  • Can you please share your controller class definition and maybe the rest of your controller? Commented Jan 19, 2019 at 12:49
  • where's that pdf_string method defined? in the controler or in a helper file? it looks like you are defining it inside a helper file, which gets included in the view which does not have a render_to_string method, you can define it on the controller and add a helper_method :pdf_string line after it so that's used in the right context Commented Jan 19, 2019 at 15:40
  • I founded out the issue, will post the answer. Commented Jan 20, 2019 at 18:43

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.