Forum: Ruby on Rails how I can use date from form via params ?

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.
Fcb55a782937626dec6e02a311075b74?d=identicon&s=25 Walde Man (alabama)
on 2007-05-15 22:29
Hi everybody !
Is somebody so polite and tell me, how I can use date from form via
params ?
For egzample:
I have form "customer".
When I have to use in my controller customer name, I can write :
@name = params[:customer][:name]
But I haven't idea how I can write date like "birthday", which is in
form as
 <%= date_select(:customer, :birthday) %>
When I wrote in controller:
@birthday = params[:customer][:birthday]
it doesn't work.
Date is divide in params on tree parts like  birthday(1i), birthday(2i)
birthday(3i) but I don't know what is proper format to use it via
params.
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-05-15 23:09
(Received via mailing list)
>  <%= date_select(:customer, :birthday) %>

As far as I understand it Rails is expecting :customer to be an
ActiveRecord subclass and :birthday to be an attribute of Customer.
When using params[:customer] in your controller for "mass assignment"
it will take care of setting my_customer.birthday automatically for
you.

Sorry, I don't know if Rails supplies a convenient way to parse this
into a Date object individually.  But, it likely has something for
that.

On May 15, 4:29 pm, Walde Man <rails-mailing-l...@andreas-s.net>
Fcb55a782937626dec6e02a311075b74?d=identicon&s=25 Walde Man (alabama)
on 2007-05-16 14:38
Robert Walker wrote:

> Sorry, I don't know if Rails supplies a convenient way to parse this
> into a Date object individually.  But, it likely has something for
> that.

Thanks for your endeavor, Robert.

Because I wasn't operated on ActiveRecord model, I used another helper
-> "select_date" with option ":prefix" in order to distinguish more than
one date. Then, it wasn't so difficult parse date via params.

Best regards, Walde.
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-05-16 21:57
(Received via mailing list)
On 5/16/07, Walde Man <rails-mailing-list@andreas-s.net> wrote:
>
> Robert Walker wrote:
>
> > Sorry, I don't know if Rails supplies a convenient way to parse this
> > into a Date object individually.  But, it likely has something for
> > that.

class Time
  class << self
    # Used for getting multifield attributes like those generated by a
    # select_datetime into a new Time object. For example if you have
    # following <tt>params={:meetup=>{:"time(1i)=>..."}}</tt> just do
    # following:
    #
    # <tt>Time.parse_from_attributes(params[:meetup], :time)</tt>
    def parse_from_attributes(attrs, field, method=:gm)
      attrs = attrs.keys.sort.grep(/^#{field.to_s}\(.+\)$/).map { |k|
attrs[k] }
      attrs.any? ? Time.send(method, *attrs) : nil
    end
  end
end

--
Rick Olson
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
8e9784d64e195904153512fd08ebe73f?d=identicon&s=25 Chewbie (Guest)
on 2007-06-19 00:11
(Received via mailing list)
Hi,

I have exactly the same problem. I am using a form separated from any
model, and I use a date object. Then I send by mail the information
contained in this form.
However when I access to my date object in the template for the mail I
have a string looking like "(3i)2(1i)2007(2i)1(1i)
(3i)2(1i)2007(2i)1(2i)(3i)2(1i)2007(2i)1(3i)".

I think the previous answer is what I am looking for but I can get it
to work, the method is not found...

Any help or suggestion to work over this?

Thanks
Chewbie
This topic is locked and can not be replied to.