Render ERB Template in RABL Template

I have a script in which I would like to pass a long message with my JSON. Instead of writing it with a concatenation string, I would prefer to put together an erb template that I can display in my JSON. Below is the code I'm trying now:

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

Everything works as expected when I run this code for the first time, however I run into problems the second time I run it because it says that there is a missing instance variable (which I suppose was generated and cached the first time launch).

undefined method _app_views_invitation_mailer_rsvp_sms_text_erb___2510743827238765954_2192068340 for # (ActionView :: Template :: Error)

Is there a better way to render erb patterns in rabl?

+5
1

ERB , :

object @invitation

node(:phone_message) do |invitation| 
  begin
    template = ERB.new(File.read("path/to/template.erb"))
    template.result(binding)
  end
end

binding - ( ) , , (@invitation )

Update:

, ( , , ), ERB :

view = ActionView::Base.new(ActionController::Base.view_paths, {})  

class << view  
 include ApplicationHelper
 include Rails.application.routes.url_helpers
end  
Rails.application.routes.default_url_options = ActionMailer::Base.default_url_options
view.render(:file => "path/to/template.html.erb", :locals => {:local_var => 'content'}) 

, Rabl.

+2

All Articles