Question

I am trying to render html in my model (for Mandrill inline code) but i cant seem to get the url_helpers to render correctly

questions.haml

 %a{:href => email_question_url(question)}

model.rb

  view = ActionView::Base.new(ActionController::Base.view_paths, @email_vars)
  view.extend ApplicationHelper
  questions_html = view.render(:partial => 'transactional_mailer/questions_html')

I ideally want to run:

      view.include Rails.application.routes.url_helpers

But that bombs out with undefined method include for actioniew::base

Any suggestions on how i could approach this?

Était-ce utile?

La solution

I had to open the instance class and include the instance methods within it via:

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

  class << view
    include Rails.application.routes.url_helpers
  end

  questions_html = view.render(:partial => 'transactional_mailer/questions_html')

Autres conseils

In my case, where I needed to test (via RSpec) a class that required a reference to the view instance inside of it, I did the following:

let(:view) do
  ActionView::Base.new(ActionController::Base.view_paths, {}).tap do |view_instance|
    view_instance.instance_eval { self.class.include Rails.application.routes.url_helpers }
  end
end

It worked for me. It doesn't look nice but it works.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top