If i have a select_date in my view like this : select_date @end_date,:prefix=>"end_date" Then I parse it as follows in the controller: @end_date Time.parse(params["start_date"][:day]+"-"+params["start_date"][:month]+"-"+params["start_date"][:year]).to_date This somehow doesn''t seem right. What is the best method to do it? Thanks Chris -- 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 -~----------~----~----~----~------~----~------~--~---
Tom Lianza
2006-Sep-08 17:44 UTC
Re: Which is the most ruby style way to do this? Rubies only
Chris wrote:> If i have a select_date in my view like this : > > select_date @end_date,:prefix=>"end_date" > > Then I parse it as follows in the controller: > > @end_date > Time.parse(params["start_date"][:day]+"-"+params["start_date"][:month]+"-"+params["start_date"][:year]).to_date > > > This somehow doesn''t seem right. What is the best method to do it?My method isn''t much better (maybe an expert can clarify us both) but you can shave off a little work, and enforce a little more structure, by using the date object instead of the time object: Date.new(params["start_date"][:year].to_i, params["start_date"][:month].to_i, params["start_date"][:day].to_i) -- 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 -~----------~----~----~----~------~----~------~--~---
Kent Sibilev
2006-Sep-08 18:36 UTC
Re: Which is the most ruby style way to do this? Rubies only
On 9/8/06, Tom Lianza <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Date.new(params["start_date"][:year].to_i, > params["start_date"][:month].to_i, params["start_date"][:day].to_i)or Date.new(*params["start_date"].values_at(*%w[year month day]).map(&:to_i)) I haven''t tried it though. -- Kent --- http://www.datanoise.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 -~----------~----~----~----~------~----~------~--~---
Cayce Balara
2006-Sep-08 18:54 UTC
Re: Which is the most ruby style way to do this? Rubies only
Depending on what you are doing, there may be an easier way. If you just want to update a table in your database, then you should use date_select instead of select_date. The date_select helper will hand the data off to ActiveRecord in a format that AR can handle for you. <%= date_select ''controller'' ''method'' %> This will bring the data back parsed out as follows: {"date(1i)"=>"2006", "date(2i)"=>"9", "date(3i)"=>"8"} ... and you can call object.update_attributes params[:object] to have ActiveRecord do all the work to make it a date and get it into the database. If, instead, you need to do something with that value besides putting it into a model, then the way you or the other respondent are doing it is pretty much it. You could dig into ActiveRecord to find the code that translates this, also. c. Chris wrote:> If i have a select_date in my view like this : > > select_date @end_date,:prefix=>"end_date" > > Then I parse it as follows in the controller: > > @end_date > Time.parse(params["start_date"][:day]+"-"+params["start_date"][:month]+"-"+params["start_date"][:year]).to_date > > > This somehow doesn''t seem right. What is the best method to do it? > > Thanks > Chris-- 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 -~----------~----~----~----~------~----~------~--~---