How call distance_of_time_in_words() from a controller instead of a view? Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M001712 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 2) 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dan Manges
2008-Sep-08 00:34 UTC
Re: How call distance_of_time_in_words() from controller?
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 Alvin <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> How call distance_of_time_in_words() from a controller instead of a > view? > > Reference:http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html... > > 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 > > 2) 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter Alvin
2008-Sep-08 11:58 UTC
Re: How call distance_of_time_in_words() from controller?
> include ActionView::Helpers::DateHelperThat''s what I needed; it worked; thanks!!! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Russell Norris
2008-Sep-08 12:28 UTC
Re: How call distance_of_time_in_words() from controller?
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 [http://en.wikipedia.org/wiki/Model-view-controller] 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 Alvin < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > include ActionView::Helpers::DateHelper > > That''s what I needed; it worked; thanks!!! > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Pardee, Roy
2008-Oct-07 15:11 UTC
Re: How call distance_of_time_in_words() from controller?
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 -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Neil Funk Sent: Monday, October 06, 2008 6:15 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: How call distance_of_time_in_words() from controller? 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Roy Pardee 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---