1
votes
module AHelper
   class Builder
     def link
       build_link(@apple)
     end
   end
   def build_link(sth)
     link_to "#", "buy #{sth}"
   end
end

In a helper module, link_to method is available to be invoked but inside the class Builder, it isn't. So I'm looking a way to call external method included in a helper module and passing a instance variable value to this method.

I don't want to include ActionView::UrlHelper into the class as it is already available to the helper module. And link_to is just an example case to demo the needs here. How to do this in ruby on rails?

1
Not duplicate since link_to is only an example in this question.Albin

1 Answers

3
votes

You can achieve this by passing the view context to the instance of Builder

module AHelper
    class Builder
      def initialize(view)
        @view = view
      end
      def link
        @view.build_link(@apple)
      end
    end
    def build_link(sth)
      link_to "#", "buy #{sth}"
    end
end

And when you instantiate the builder you pass in view_context if it is in a controller or self if it is in a helper or view.