Hi I have two date fields in table start_date and end_date .There data type are datetime. Now in the view I am accepting values of them as text fields like And I am following RailsCast 32 <%= f.text_field :start_date_string, :value => '''', :maxlength => 50 %><img src="../images/calendar.png" alt="Calendar" /> <%= f.text_field :end_date_string, :value => '''', :maxlength => 50 %><img src="../images/calendar.png" alt="Calendar" /> And in my model validates_presence_of :start_date def start_date_string start_date.to_s(:db) end def start_date_string=(start_date_str) self.start_date = Time.parse(start_date_str) end def end_date_string end_date.to_s(:db) end def end_date_string=(end_date_str) self.end_date = Time.parse(end_date_str) end But my problem is validates_presence_of :start_date is not working Even if I dont give any values to start date it does not show any error message. Please help Thanks in advance Tom -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 April 2010 13:10, Tom Mac <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi > I have two date fields in table start_date and end_date .There data > type are datetime. Now in the view I am accepting values of them as > text fields like And I am following RailsCast 32 > > <%= f.text_field :start_date_string, :value => '''', :maxlength => 50 > %><img src="../images/calendar.png" alt="Calendar" /> > <%= f.text_field :end_date_string, :value => '''', :maxlength => 50 %><img > src="../images/calendar.png" alt="Calendar" /> > > And in my model > > validates_presence_of :start_date > > def start_date_string > start_date.to_s(:db) > end > > def start_date_string=(start_date_str) > self.start_date = Time.parse(start_date_str) > end > > def end_date_string > end_date.to_s(:db) > end > > def end_date_string=(end_date_str) > self.end_date = Time.parse(end_date_str) > end > > > But my problem is validates_presence_of :start_date is not working > Even if I dont give any values to start date it does not show any error > message. Please helpWhen you say it is not working, what exactly is not working? Two things should happen if the validate fails, firstly the record should not get saved to the database and secondly an error message should be made available. You have said that you are seeing no message but is the record getting saved or not? How are you showing the error message? An error here could be the explanation for not seeing the message. Have you tried using ruby-debug to break into your controller and see what is happening when you submit? If you do not know how to do this have a look at the rails guide on debugging at http://guides.rubyonrails.org/. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 1, 10:49 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 1 April 2010 13:10, Tom Mac <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > > > Hi > > I have two date fields in table start_date and end_date .There data > > type are datetime. Now in the view I am accepting values of them as > > text fields like And I am following RailsCast 32 > > > <%= f.text_field :start_date_string, :value => '''', :maxlength => 50 > > %><img src="../images/calendar.png" alt="Calendar" /> > > <%= f.text_field :end_date_string, :value => '''', :maxlength => 50 %><img > > src="../images/calendar.png" alt="Calendar" /> > > > And in my model > > > validates_presence_of :start_date > > > def start_date_string > > start_date.to_s(:db) > > end > > > def start_date_string=(start_date_str) > > self.start_date = Time.parse(start_date_str) > > end > > > def end_date_string > > end_date.to_s(:db) > > end > > > def end_date_string=(end_date_str) > > self.end_date = Time.parse(end_date_str) > > end > > > But my problem is validates_presence_of :start_date is not working > > Even if I dont give any values to start date it does not show any error > > message. Please help > > When you say it is not working, what exactly is not working? Two > things should happen if the validate fails, firstly the record should > not get saved to the database and secondly an error message should be > made available. You have said that you are seeing no message but is > the record getting saved or not? > > How are you showing the error message? An error here could be the > explanation for not seeing the message. > > Have you tried using ruby-debug to break into your controller and see > what is happening when you submit? If you do not know how to do this > have a look at the rails guide on debugging athttp://guides.rubyonrails.org/. > > ColinHi Team, I am having a similar problem, also following along from railscast 32. Any suggestions welcome. Is it an erb thing or something wrong with my model code? Also :formdate is a date field not a datetime field. =========View throws this ERROR PAGE: ArgumentError in Contracts#new Showing app/views/contracts/new.html.erb where line #84 raised: wrong number of arguments (1 for 0) Extracted source (around line #84): 81: <td> 82: <div class="order_details"> 83: 84: <div class="onecol"><%= f.label :formdate, "Date" %><br /><%f.text_field :formdate_string 85: %></div> 86: <div class="twocol1"><%= f.label :publication %> <%f.text_field :publication %></div> 87: <div class="twocol2"><%= f.label :issue %> <%f.text_field :issue %></div> Request Parameters: None Show session dump --- Response Headers: {"Content-Type"=>"text/html", "Cache-Control"=>"no-cache"} ========= MODEL: class Contract < ActiveRecord::Base def formdate_string formdate.to_s(:db) end def formdate_string=(fdate_str) self.formdate = Time.parse(fdate_str) end end ========= VIEW: new.html.erb - extract <div class="onecol"><%= f.label :formdate, "Date" %><br /> <%= f.text_field :formdate_string %></ div> ========= Thank you. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 14 April 2010 03:47, Cote <prussell-1YXwN4Z2WqE+hnYoyCZ4RTSf8X3wrgjD@public.gmane.org> wrote:>... > Hi Team, > > I am having a similar problem, also following along from railscast 32. > > Any suggestions welcome. Is it an erb thing or something wrong with my > model code? Also :formdate is a date field not a datetime field. > > =========> View throws this ERROR PAGE: > ArgumentError in Contracts#new > > Showing app/views/contracts/new.html.erb where line #84 raised: > > wrong number of arguments (1 for 0) > > Extracted source (around line #84): > > 81: <td> > 82: <div class="order_details"> > 83: > 84: <div class="onecol"><%= f.label :formdate, "Date" %><br /><%> f.text_field :formdate_string > 85: %></div> > 86: <div class="twocol1"><%= f.label :publication %> <%> f.text_field :publication %></div> > 87: <div class="twocol2"><%= f.label :issue %> <%> f.text_field :issue %></div>This is not a similar problem at all. The OP had an issue with validations, yours is showing an error. I don''t see the problem immediately, but I suggest removing bits of code till it does not show the error, then putting it back a little at a time till you find exactly what is causing the problem.> > > Request > > Parameters: > > None > > Show session dump > > --- > > Response > > Headers: > > {"Content-Type"=>"text/html", > "Cache-Control"=>"no-cache"} > =========> > MODEL: > class Contract < ActiveRecord::Base > > def formdate_string > formdate.to_s(:db) > end > > def formdate_string=(fdate_str) > self.formdate = Time.parse(fdate_str) > end > > end > =========> > VIEW: new.html.erb - extract > <div class="onecol"><%= f.label :formdate, "Date" %><br /> > <%= f.text_field :formdate_string %></ > div>That does not appear to be exactly the same code as shown in the error above. Above it has a newline after :formdate_string, as it shows the %> on line 85. Could that be part of the problem I wonder? Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 14, 10:46 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 14 April 2010 03:47, Cote <pruss...-1YXwN4Z2WqE+hnYoyCZ4RTSf8X3wrgjD@public.gmane.org> wrote: > > > > >... > > Hi Team, > > > I am having a similar problem, also following along from railscast 32. > > > Any suggestions welcome. Is it an erb thing or something wrong with my > > model code? Also :formdate is a date field not a datetime field. > > > =========> > View throws this ERROR PAGE: > > ArgumentError in Contracts#new > > > Showing app/views/contracts/new.html.erb where line #84 raised: > > > wrong number of arguments (1 for 0) > > > Extracted source (around line #84): > > > 81: <td> > > 82: <div class="order_details"> > > 83: > > 84: <div class="onecol"><%= f.label :formdate, "Date" %><br /><%> > f.text_field :formdate_string > > 85: %></div> > > 86: <div class="twocol1"><%= f.label :publication %> <%> > f.text_field :publication %></div> > > 87: <div class="twocol2"><%= f.label :issue %> <%> > f.text_field :issue %></div> > > This is not a similar problem at all. The OP had an issue with > validations, yours is showing an error. > > I don''t see the problem immediately, but I suggest removing bits of > code till it does not show the error, then putting it back a little at > a time till you find exactly what is causing the problem. > > > > > > > Request > > > Parameters: > > > None > > > Show session dump > > > --- > > > Response > > > Headers: > > > {"Content-Type"=>"text/html", > > "Cache-Control"=>"no-cache"} > > =========> > > MODEL: > > class Contract < ActiveRecord::Base > > > def formdate_string > > formdate.to_s(:db) > > end > > > def formdate_string=(fdate_str) > > self.formdate = Time.parse(fdate_str) > > end > > > end > > =========> > > VIEW: new.html.erb - extract > > <div class="onecol"><%= f.label :formdate, "Date" %><br /> > > <%= f.text_field :formdate_string %></ > > div> > > That does not appear to be exactly the same code as shown in the error > above. Above it has a newline after :formdate_string, as it shows the > %> on line 85. Could that be part of the problem I wonder? > > ColinThanks for your prompt reply. It wasn''t the new line, but that difference was well spotted. I found it... makes sense now... ArgumentError... it wasn''t getting passed an argument for a new form. The form worked for the edit action when the field was populated. I needed to initialize the formdate field with a value. So I added this to the "new" action of the controller. eg. def new @contract = Contract.new @contract.formdate = Time.now.localtime.strftime("%Y-%m-%d") end Would you say this is good practice? or could I do it better elswhere or with different code? Cheers. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi> MODEL: > class Contract < ActiveRecord::Base > > def formdate_string > formdate.to_s(:db) > end > > def formdate_string=(fdate_str) > self.formdate = Time.parse(fdate_str) > end > > endThis happens when in the new does not get value And as one of the post in the railscast page suggest it can be solved with an instance varibale like def formdate_string @formdate_string || (formdate.to_s(:db) unless formdate.nil?) end def formdate_string=(fdate_str) @formdate_string = fdate_str self.formdate = Date.parse(@formdate_string) rescue ArgumentError @formdate_invalid = true end @formdate_invalid can be used in def validate Tom -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ok Cool, thank you. On Apr 16, 2:31 pm, Tom Mac <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi > > > MODEL: > > class Contract < ActiveRecord::Base > > > def formdate_string > > formdate.to_s(:db) > > end > > > def formdate_string=(fdate_str) > > self.formdate = Time.parse(fdate_str) > > end > > > end > > This happens when in the new does not get value And as one of the > post in the railscast page suggest it can be solved with an instance > varibale like > > def formdate_string > @formdate_string || (formdate.to_s(:db) unless formdate.nil?) > end > > def formdate_string=(fdate_str) > @formdate_string = fdate_str > self.formdate = Date.parse(@formdate_string) > rescue ArgumentError > @formdate_invalid = true > end > > @formdate_invalid can be used in def validate > > Tom > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.