Forum: Ruby on Rails Controller helper_methods in template

Posted by amkirwan (Guest)
on 2012-12-03 22:24
(Received via mailing list)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.