Basic Question - From where are helpers available

Newbie question -- I thought that helpers were available in both views and controllers. The methods I'm putting in application_helper.rb don't seem to be available in the controllers. Am I missing something?

The helper methods are only available to the views.

Method heirarchy goes a bit like this:

Model Controller Helper View

With all the elements being able to access any method from any element above it.

Don’t put model code in the view because it will slow your application down. If you’re doing something like a Model.find, stick it in your controller. If you’re defining a new method for an object of Model, stick it in the model file. Helpers are methods you can call from the view to dry up your views. For example if you wanted to duplicate the e’s in a sentence such as: “The quick brown fox jumped over the lazy dog”, put this in your view:

<%= duplicate_e(“The quick brown fox jumped over the lazy dog”) %>

and this in your helper:

def duplicate_e(string) string.gsub(“e”,“ee”) end

Ryan,

You're the best!

Thanks!

But if you want, you can define the method in the controller and "export" it using

helper_method()

and then you can use it in the view.

So in your controller, you could do something like

helper_method :my_method

def my_method

end

http://wiki.rubyonrails.org/rails/pages/HowtoWorkWithHelpers

Peace, Phillip

Phillip Koebbe wrote:

If you put a method in the application.rb it should be available to all controllers that descend from ApplicationController, but referencing it in a view won’t work.

I just put this in application.rb

helper_method :say_hi

def say_hi   "hi" end

and create a test.rhtml for an arbitrary controller that consisted of

<%= say_hi %>

and when I ran http://localhost:3000/arbitrary_controller/test, I saw "hi".

So it seems that it does, in fact, work. At least with Rails 1.2.3.

Peace, Phillip