How call distance_of_time_in_words() from controller?

How call distance_of_time_in_words() from a controller instead of a
view?

Reference:

This page says this method is in: Module ActionView::Helpers::DateHelper

which also says is here:

in: actionpack/lib/action_view/helpers/date_helper.rb

BUT, HOW IN THE WORLD do I get that included in my controller file?

  1. This doesn’t work inside the class definition:

require “date_helper”

as I get this message:

MissingSourceFile in MyController#datePage

no such file to load – date_helper

  1. THEN, when I try this:

helper :date_helper

I get this:

Missing helper file helpers/date_helper_helper.rb

Please help! I bet this is cake if I just know the solution.

Thanks very much in advance,

Pete

You could do it this way.

class MyController
include ActionView::Helpers::DateHelper

def some_method
distance_of_time_in_words …
end
end

I think this would also work, and would be better since it wouldn’t be
adding all of DateHelper’s methods to the controller:

def some_method
@template.distance_of_time_in_words
end

-Dan
http://www.dcmanges.com

On Sep 7, 12:30 pm, Peter A. [email protected]

include ActionView::Helpers::DateHelper

That’s what I needed; it worked; thanks!!!

You shouldn’t really need to call this within a controller. Rails uses a
convention called MVC which means that the model, the view, and the
controller concerns are all separate to themselves. Here’s the wikipedia
article on it [Model–view–controller - Wikipedia] and
here’s an entry in the Rails’ wiki for it as well [
http://wiki.rubyonrails.org/rails/pages/UnderstandingMVC]. It’s a really
core convention in rails. distance_of_time_in_words is purely a view
concern
because it’s about output which is the view. The standard way to do this
is
to render a template [view] instead of constructing a string directly in
the
controller. Hope that helps.

RSL

On Mon, Sep 8, 2008 at 7:58 AM, Peter A. <

RSL ___ wrote:

distance_of_time_in_words is purely a view concern
because it’s about output which is the view.

Thanks, Dan and RSL. I think Pete might have been asking because of the
same problem I am having right now: I know what MVC is, but I just
can’t for the life of me figure out how to subtract 2 dates! I’m pulling
at straws here to find any hack that might work and this
distance_of_time_in_words() is the closest I’ve gotten. I have 2 fields
in an ActiveRecord model, due:date and complete:date, that I’m trying to
find the elapsed time between. I’d think this is just a simple matter
of subtraction, but apparently not. I have a strong background in C and
Java and Python, but just recently got into Ruby (from Rails). All I’m
trying to do is subtract these 2 fields for every instance of the model.
This should be the simplest thing ever, but I’m missing something.
Could someone please please clue me in? Thanks.
-Neil

Roy P. wrote:

It looks to me like you can subtract one date var from another & get a
Rational that represents the number of days difference. You should be
able to .to_i that to work w/it as a number.

Thanks, Roy. You are quite right. The reason it wasn’t working for me
before was that I was making a silly ruby-n00b mistake of using the
symbols for the member variables, rather than the variables themselves!
( :completed - :due instead of completed - due) I didn’t do that
anywhere else so I don’t know what I was thinking.

It looks to me like you can subtract one date var from another & get a
Rational that represents the number of days difference. You should be
able to .to_i that to work w/it as a number.

HTH,

-Roy