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

On Jan 10, 2008 9:26 AM, Decker [email protected] wrote:

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?


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

Ryan,

You’re the best!

Thanks!

On Jan 9, 2008, at 5:04 PM, Ryan B. wrote:

The helper methods are only available to the views.

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

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.

On Jan 10, 2008 2:24 PM, Jeff P.
[email protected]
wrote:

helper_method()

thanks,
jp


Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

On Jan 9, 2008, at 9:54 PM, Jeff P. wrote:

helper_method()

thanks,
jp

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

Phillip K. wrote:

On Jan 9, 2008, at 5:04 PM, Ryan B. wrote:

The helper methods are only available to the views.

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

Hmmmn, what if you put a method in the “application.rb” controller and
then used helper_method() on it? Would it then be available to all
controllers and all views?

thanks,
jp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs