Ruby on Rails Monday, December 3, 2012

I'm trying to render a rails form using HAML outside of the normal Rails flow using the code below:


  template = File.join("#{Rails.root}", 'lib', 'templates') 
  context = ActionView::Base.new(template, {}, @controller, nil)
  context.class_eval do
    include Rails.application.routes.url_helpers
    inlcude Rails.application.routes.mounted_helpers
   end 
  Haml::Engine.new(template, :attr_wrapper => '\').render(context, { :@page => @page })

The problem is I keep receiving the following error undefined method `protect_against_forgery?' for #<ActionView::Base:0x007fcd05b52fa0>

For some reason the helper_methods defined in the controller are not being set on the ActionView::Base instance and I'm not sure how to set them or how Rails goes about doing it. I've looked through the source code but don't see how it is wired together. That said if I manually add the methods using class_eval then it all works the template renders correctly

  def protect_against_forgery?
    @controller.send(:protect_against_forgery?) 
  end

   def form_authenticity_token 
     @controller.send(:form_authenticity_token) 
   end

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/7akt9KHpIIkJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment