Question

I have a helper method that I need to use on views and controllers. For example, a method to draw a PayPal url...

def paypal_url
   "https://www.paypal.com/cgi-bin/webscr"
end

then in some controller I could have something like:

redirect_to paypal_url

In some view I could have:

 <%= form_tag(paypal_url) %>

The question is: where should I put this method?

I know that I can include view helpers into controllers. That's not my question...

Was it helpful?

Solution

Define it in the ApplicationController like:

class ApplicationController < ActionController::Base

  helper_method :paypal_url

  def paypal_url
    'https://www.paypal.com/cgi-bin/webscr'
  end

end

After that you'll be able to use the helper method from any view or controller.

OTHER TIPS

You should read about the presenter pattern. There's a very good railscast about it but you can find infomation from many sources with a quick google search.

In a nutshell, the presenter pattern is a bridge between Model and View of the rails MVC design. The idea is that model logic doesn't belong in the controller (and indeed, the controller shouldn't know about the models it's serving), and it certainly doesn't belong in the view.

the presenter pattern offers a way to tie model logic into the view without compromising the controller

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top