Help with Helpers

I’d like to know where exactly are helpers available by default?

For example, if I have a PagesController, will my PagesHelper methods be available at PagesController? or just at the Pages’s views?

Thank you,

Rodrigo

Helpers help generate code for the view. For example, if you have an array of items that need to be rendered in a specific way, you can encapsulate that functionality in a helper method, and call it from the view. It helps clean up the view code, and adds code reuse. So helpers are meant to be for views, not controllers.

Helpers help generate code for the view. For example, if you have an array of items that need to be rendered in a specific way, you can encapsulate that functionality in a helper method, and call it from the view. It helps clean up the view code, and adds code reuse. So helpers are meant to be for views, not controllers.

> I'd like to know where exactly are helpers available by default?

> For example, if I have a PagesController, will my PagesHelper methods be > available at PagesController? or just at the Pages's views?

Dheeraj is correct, helpers are intended for views, but if you have a method that you need access to in your controller and your view, and/ or your helper then, in order to keep things a little DRYer, you can define your method in your controller, and make it available to the view and helper as so:

class MyController < ApplicationController

  helper_method :my_method

  private

    def my_method       # do something     end

end

Now you can access my_method from your view or your helper as well as your controller.

hth

Paul

In addition to other replies note that logic in controllers should rarely be sufficiently complex so as to require helpers. Such code should almost always be in the models.

Colin