Forum: Ruby on Rails How do form data get into ActiveRecord object?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alex (Guest)
on 2007-03-26 18:06
(Received via mailing list)
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
Bill W. (Guest)
on 2007-03-26 18:38
(Received via mailing list)
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
Zack C. (Guest)
on 2007-03-26 18:49
(Received via mailing list)
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
Alex (Guest)
on 2007-03-27 10:09
(Received via mailing list)
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
This topic is locked and can not be replied to.