Hello, I''m new to this forum and also I''m new to programming, to Ruby and to Ruby on Rails. I''m from Russia and started learingn RoR with book "Agile Web Development with Rails". Right now I''m trying to practice in building web applications using Ruby on Rails. So, question: I''ve scaffolded DB with following structure: create_table(:products) do |t| t.column :title, :string t.column :description, :text t.column :imageurl, :string t.column :price, :decimal, :default => 0, :precision => 8, :scale => 2 t.column :date, :date end Then I''m trying to validate input data; everything is great, but I can''t validate input :date. I''m always getting error about :date. (Don''t ask why I''m trying to validate date - I''m just trying to have some practice.) def validate date_pattern = /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/ errors.add(:date, "Date must be formatted YYYY-MM-DD #{date.to_s}") if date.nil? || date_pattern =~ date.to_s end When I''m trying to make date_pattern =~ "2007-01-15" it''s ok; it is 0 but why I''m getting error when trying to validate :date? Hope to hear from you soon - thanks! -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
P.S. Sorry for my english. I know it is awful. :) But I hope it will be clear for you. Thanks! -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
You need !~, not =~. Note that 0 is NOT false, it is true, so puts "true" if 0 will print "true" Might I suggest instead using: Date.parse(date) instead? This will also make certain the dates are valid, and will accept things like 07-10-10 as well. You will need to protect that call inside of a begin ... rescue ... end block though since it will raise an exception on a format / data error. --Michael --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Michael, Thanks. Got it! There is no info about Date.parse() in my Ruby book. Please, could you gimme URL where I can read about it. I''m real noob and I need some practice in finding manuals etc. for different classes and functions. I don''t have enough bookmarks about Rails which I can refer to. Thanks! -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
I''d start with: http://www.ruby-doc.org/core/ Scroll down in the middle column, and select "Date" to display information about that class. --Michael --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/15/07, Michael Graff <skan.gryphon-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > You need !~, not =~. Note that 0 is NOT false, it is true, so > > puts "true" if 0 > > will print "true" > > Might I suggest instead using: > > Date.parse(date) > > instead? This will also make certain the dates are valid, and will > accept things like 07-10-10 as well. You will need to protect that > call inside of a begin ... rescue ... end block though since it will > raise an exception on a format / data error.Or if he wants to use the regexp validates_format_of :date :with => /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/ -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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?hl=en -~----------~----~----~----~------~----~------~--~---
Rick, thanks. I''ve noticed this method too at http://www.ruby-forum.com/topic/127958#new Michael, Philip, thanks for useful links! Thumbs up! -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
> Michael, Thanks. Got it! > > There is no info about Date.parse() in my Ruby book. > Please, could you gimme URL where I can read about it.http://www.ruby-lang.org/en/documentation/ in particular... http://www.ruby-doc.org/core/ http://www.ruby-doc.org/stdlib/ and specifically: http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/index.html (far right top frame, scroll to the parse method). For myself, I keep a local copy of the above and then link directly to the method frame like this: http://www.ruby-doc.org/core/fr_method_index.html Then a search for ''parse'' would find what I want without having all those little frames :)> > I''m real noob and I need some practice in finding manuals etc. for > different classes and functions. > > I don''t have enough bookmarks about Rails which I can refer to. > > Thanks! > > -- > 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 15, 3:42 pm, Kir Izoid <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> P.S. Sorry for my english. I know it is awful. :) But I hope it will be > clear for you. > > Thanks! > > -- > Posted viahttp://www.ruby-forum.com/.FYI: there''s a couple neat libraries for parsing/validating dates/times: http://runt.rubyforge.org/ http://chronic.rubyforge.org/ and your English is fine! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Gene, thanks. Great links! Especially I liked "Chronic" with it''s great features like Chronic.parse(''this tuesday 5:00'') etc. Very useful. I will bookmark it and will refer to this libraries every time when I need to parse or validate dates/times. Thanks. -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 16, 1:53 am, "Kirizoid B." <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Gene, thanks. Great links! Especially I liked "Chronic" with it''s great > features like Chronic.parse(''this tuesday 5:00'') etc. > > Very useful. I will bookmark it and will refer to this libraries every > time when I need to parse or validate dates/times. > > Thanks. > > -- > Posted viahttp://www.ruby-forum.com/.one of the basic principles of ruby/rails programming is to look for a plugin, gem or lib (or maybe a pastie) of some sort for a well-defined requirement before you spend 3 days and nights keying and mousing away. People have thought of nearly everything . --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
This is making more sense now, my date field in my model is persisted as a date in the database. From my understanding of all of your posts, there is no way to validate the input because it is an instance of a date, not an instance of a string. Therefore when ruby converts the date to the ISO string, its not possible to get it into the format I expect during the validation phase. Are there any formatting hooks in RoR? In other words, can I register input and output formatters (Similar to JSF in Java) so that dates aren''t displayed with the ISO format? Since my users will mostly be in the US, I don''t want to always have the format as yyyy-mm-dd. My users would much rather have mm/dd/yyyy, so I would like to have that format as both the input and output, then I can wrap it with a javascript calendar to make it the input easy to use. I''ve tried Googling online for some documentation outlining the request lifecycle of a RoR app, but I can''t seem to find what I need. Thanks, Todd On Oct 16, 11:33 pm, gene tani <gene.t...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Oct 16, 1:53 am, "Kirizoid B." <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Gene, thanks. Great links! Especially I liked "Chronic" with it''s great > > features like Chronic.parse(''this tuesday 5:00'') etc. > > > Very useful. I will bookmark it and will refer to this libraries every > > time when I need to parse or validate dates/times. > > > Thanks. > > > -- > > Posted viahttp://www.ruby-forum.com/. > > one of the basic principles of ruby/rails programming is to look for a > plugin, gem or lib (or maybe a pastie) of some sort for a well-defined > requirement before you spend 3 days and nights keying and mousing > away. People have thought of nearly everything .--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Oct 18, 5:00 pm, Todd Nine <todd.n...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is making more sense now, my date field in my model is persisted > as a date in the database. From my understanding of all of your > posts, there is no way to validate the input because it is an instance > of a date, not an instance of a string. Therefore when ruby converts > the date to the ISO string, its not possible to get it into the format > I expect during the validation phase. Are there any formatting hooks > in RoR? In other words, can I register input and output formatters > (Similar to JSF in Java) so that dates aren''t displayed with the ISO > format? Since my users will mostly be in the US, I don''t want to > always have the format as yyyy-mm-dd. My users would much rather have > mm/dd/yyyy, so I would like to have that format as both the input and > output, then I can wrap it with a javascript calendar to make it the > input easy to use. I''ve tried Googling online for some documentation > outlining the request lifecycle of a RoR app, but I can''t seem to find > what I need. > > Thanks, > Todd > > On Oct 16, 11:33 pm, gene tani <gene.t...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Oct 16, 1:53 am, "Kirizoid B." <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > > > > Gene, thanks. Great links! Especially I liked "Chronic" with it''s great > > > features like Chronic.parse(''this tuesday 5:00'') etc. > > > > Very useful. I will bookmark it and will refer to this libraries every > > > time when I need to parse or validate dates/times. > > > > Thanks. > > > > -- > > > Posted viahttp://www.ruby-forum.com/. > > > one of the basic principles of ruby/rails programming is to look for a > > plugin, gem or lib (or maybe a pastie) of some sort for a well-defined > > requirement before you spend 3 days and nights keying and mousing > > away. People have thought of nearly everything .request lifecycle: http://brainspl.at/request_response.pdf strftime: http://noobonrails.blogspot.com/2005/11/prim-and-proper-dates.html --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/18/07, Todd Nine <todd.nine-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > This is making more sense now, my date field in my model is persisted > as a date in the database. From my understanding of all of your > posts, there is no way to validate the input because it is an instance > of a date, not an instance of a string. Therefore when ruby converts > the date to the ISO string, its not possible to get it into the format > I expect during the validation phase. Are there any formatting hooks > in RoR? In other words, can I register input and output formatters > (Similar to JSF in Java) so that dates aren''t displayed with the ISO > format? Since my users will mostly be in the US, I don''t want to > always have the format as yyyy-mm-dd. My users would much rather have > mm/dd/yyyy, so I would like to have that format as both the input and > output, then I can wrap it with a javascript calendar to make it the > input easy to use. I''ve tried Googling online for some documentation > outlining the request lifecycle of a RoR app, but I can''t seem to find > what I need.I think that the reason dates are stored in the database in ISO format is to enable date comparisons. "2007-10-19" > "2006-12-08" but "10/19/2007" < "12/08/2006" But if the column type is time, date or datetime, ActiveRecord will convert to and from Ruby Time and Date objects when you read and write the record. You can then format as you wish using the strftime method. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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?hl=en -~----------~----~----~----~------~----~------~--~---
This may be of interest http://www.railslodge.com/plugins/111-validates-date-time -- 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?hl=en -~----------~----~----~----~------~----~------~--~---