Josh Hadik
Josh Hadik

Reputation: 433

How can I pass rails helper methods to a view using ActionView::Base?

I am using ActionView::Base to render views in my current rails project, instead of ApplicationController. So far this has gone smoothly, but now I need a way to access helper methods from views rendered in this fashion. The exact code I am using to render views looks like this:

action_view = ActionView::Base.new(ActionController::Base.view_paths, {})
action_view.render(file: "example/path", locals: { foo: 1, bar: "hello world" })

Is there any way to pass custom helper methods from the helpers folder to a view being rendered in this fashion?

Upvotes: 5

Views: 1108

Answers (1)

Ruslan
Ruslan

Reputation: 2009

You can use send to include the helpers you need

action_view = ActionView::Base.new(ActionController::Base.view_paths, {})
action_view.class.send(:include, ApplicationHelper)
action_view.render(file: "example/path", locals: { foo: 1, bar: "hello world" })

Upvotes: 7

Related Questions