我有一个场景,我想用我的JSON传回一条长信息.而不是用字符串连接写出来,我宁愿把一个erb模板放在一起,我可以渲染到我的JSON中.以下是我目前正在尝试的代码:
object @invitation
node(:phone_message) do |invitation|
begin
old_formats = formats
self.formats = [:text] # hack so partials resolve with html not json format
view_renderer.render( self, {:template => "invitation_mailer/rsvp_sms", :object => @invitation})
ensure
self.formats = old_formats
end
end
Run Code Online (Sandbox Code Playgroud)
第一次运行此代码时,一切都按预期工作,但是,第二次运行时遇到问题,因为它说有一个缺少的实例变量(我假设在第一次运行时生成并缓存).
未定义的方法_app_views_invitation_mailer_rsvp_sms_text_erb___2510743827238765954_2192068340 for#(ActionView :: Template :: Error)
有没有更好的方法将erb模板呈现为rabl?