1
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?

2

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.