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.
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.