Hi I''m quite new to rails and I think until now I didn''t get all of ActiveRecord :-) So, here is what I want to do: I have a form where a user can input a date. The logic behind the date field should be a bit fancy and allow a special input short cuts: - "" (blank field) => Date.today - "25" => 25th of currnet month - "23.3" => March, 23th in current year.. and so on (I guess you got the clue) I think I could use client side javascript to do the job, but IMHO this is not a good way to do this. Where can I hook this in in Rails? For me this belongs into the modle class. In my controller I have the following line: @project_time = ProjectTime.new(params[:project_time]) The "date" is one of the params. So which method do I need to implement/override to get the chance to look at the date passed from the form ? Thanks for your help Alex --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Alex, Alex wrote:> I have a form where a user can input a date. The logic > behind the date field should be a bit fancy and allow a > special input short cuts: > > - "" (blank field) => Date.today > - "25" => 25th of currnet month > - "23.3" => March, 23th in current year..> Where can I hook this in in Rails? > > For me this belongs into the modle class.> The "date" is one of the params. So which method do I need to > implement/override to get the chance to > look at the date passed from the form ?You might want to define a method in your model to implement the logic and use a before_save filter to call it. hth, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alex, You could fiddle with the param in the controller. Here is a mock-up: now = Time.now param_date = params[:project_time][:date] params[:project_time][:date] = case when /^\d$/ then Time.mktime(now.year, now.month, param_date) ... else param_date end -- Zack Chandler http://depixelate.com On 3/26/07, Alex <kleckse-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi > > I''m quite new to rails and I think until now I didn''t get all of > ActiveRecord :-) > > So, here is what I want to do: > > I have a form where a user can input a date. The logic behind the date > field should be a bit fancy and allow a special input short cuts: > > - "" (blank field) => Date.today > - "25" => 25th of currnet month > - "23.3" => March, 23th in current year.. > > and so on (I guess you got the clue) > > I think I could use client side javascript to do the job, but IMHO > this is not a good way to do this. > > Where can I hook this in in Rails? > > For me this belongs into the modle class. > > In my controller I have the following line: > @project_time = ProjectTime.new(params[:project_time]) > > The "date" is one of the params. So which method do I need to > implement/override to get the chance to > look at the date passed from the form ? > > Thanks for your help > > Alex > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Thanks for your help. I found another solution, which I think is the way to go, as the date is corrected just as it is added to the model. So the model always holds correct data. I overwrite the date= method in my model class ProjectTime < ActiveRecord::Base ... def date=(value) # check value and do the filtering value = .... # set attribute im my model class self[:date] = value end end Alex --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---