简体   繁体   中英

Getting access to helper methods inside ApplicationController.new.render_to_string

I have a Rails 5 app. Inside my FooController , I am generating some html content via:

ApplicationController.new.render_to_string("some_haml_file", locals: {foo: "bar"}, layout: false)

Inside some_haml_file.html.haml I need to perform some complicated logic, so leveraging helper methods, or something that corresponds to it, would help a lot. A .rb file of some kind to include.

Is it possible? I suspect that the question may have been answered in this SO post , but I honestly don't understand it.

There's a helpers folder in your application. Create a new module there that's named *anything*Helper , eg FooHelper . The filename should be foo_helper.rb in that case.

File's contents would be

module FooHelper
   def useful_function
   end
end

Then you can use useful_function in your HAML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM