问题
I have a service that uses an ERB file to render a template form.
In the form I use several helpers, but they don't seem to be available in this context.
how could I use my helpers in the rendering via ERB.new(template).result(binding) ?
this is the error I'm getting:
*** NoMethodError Exception: undefined method `image_encoded' for #<MyService>
here's the call in the service app/services/my_service.rb file:
ERB.new(template).result(binding)
here's my helper app/helpers/my_helper.rb:
module MyHelper
def image_encoded(image_url)
<<image stuff>>
end
end
here's my call to the helper in the app/views/my_template/my_template.html.erb file:
<img src="<%= image_encoded(image) %>"/>
回答1:
In your service your helpers are not included by default. You can include the methods from a particular helper like this:
class MyService
include MyHelper
end
来源:https://stackoverflow.com/questions/62211842/rails-how-to-use-helpers-with-erb-new-in-service