How to pass variables to render_to_string?

前端 未结 5 624
余生分开走
余生分开走 2020-12-14 06:17

Trying to do the following

@message = render_to_string ( :sender => sender, :template => \"template\" )          

But when accessing

5条回答
  •  既然无缘
    2020-12-14 06:59

    Here's Jason Kim's solution he wrote in a comment which worked for me:

    ActionController::Base.new.render_to_string(
      "user_mailer/welcome_email.html.erb", locals: { :@user => user}
    )
    

    Please mind the :@user => value bit.


    In Rails 5 (atm in beta):

    ApplicationController.render(
      file: 'path',
      assigns: { foo: 'bar' }
    )
    

    More here

提交回复
热议问题