How do form data get into ActiveRecord object?

Hi

I’m quite new to rails and I think until now I didn’t get all of
ActiveRecord :slight_smile:

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

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

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 C.
http://depixelate.com

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