Also, how would you access controller-speific functions, as in the one I
had in my original post?
You can create a function in the controller and use the helper_method
specifier.
eg
class ApplicationController < ActionController::Base
helper_method :give_time
def give_time
Time.now # return the current time
end
end
Then in you view, you can put a
<%= give_time %>
In fact, helper_method makes a method visible both in the controller and
in the view. But, IMHO you should take care of not making wrong usage of
this specifier.
In most cases you just have to put these methods in the
application_helper.rb which makes it visible in all views.
I have a controller named aircrafts. In there I put:
def showtime @time = Time.now
end
In the default views/layouts/aircrafts.rhtml, I want to use the method
above to show the current time on the page.
But adding <%= @time %> does nothing. Do I have to have some kind of
<%= showtime.time %> syntax or something, like in Java?
Thanks. New to Ruby, and trying to learn as I go, so please forgive
some of these ‘simple’ questions.
Hussein.
Methods in the controller generally map to the view of that name. So,
if you have a file called views/showtime.rhtml with <%= @time %>, it
will display correctly.
You need to change it to <%= showtime %> if you want it to work.
However, this method should not be in the controller. It should probably
be in helpers/aircraft_helper.rb or helpers/application_helper.rb
I’m still green to rails, so there could be a better way, but both of my
options will work.