I am so close to getting the date picker to work, and I think my short comings are sql based... The helper is great and works like a champ! im either getting invalid date and general syntax problems on the two to three definitions in the controller... Basically I have one field and 2 pickers giving up two dates, I want to query all records between the two dates.... def search @logs = Log.find_by_sql [''SELECT * FROM logs WHERE user LIKE ? and accesstime > ? and accesstime < ?'', "%#{@params[''user'']}%", "%#{@params[''accesstime'']}%", "%#{@params[''accesstime2'']}%"] @logs = Log.find_by_sql [''SELECT * FROM logs WHERE user LIKE ? and BETWEEN accesstime ? and accesstime ?'', "%#{@params[''user'']}%", "%#{@params[''accesstime'']}%", "%#{@params[''accesstime2'']}%"] end My date format is also specified in the controller itself: helper :calendar before_filter( RequestPreprocessor.apply_to(''log.accesstime'') { |d| d && Date.strptime(d, ''%Y-%m-%d %l:%M:%S'') }, :only => [ :search, :index ]) Here is my form: <form action="/logs/search" method="post"> <p><label for="log_user">User</label><br/> <%= text_field ''log'', ''user'' %></p> <p> <%= calendar_field ''log'', ''accesstime'', { :class => ''date'', :field_title => ''Between2'', :button_title => ''Show calendar'' }, { :ifFormat => ''%Y-%m-%d %l:%M:%S'', :firstDay => 1, :step => 1, :showOthers => true, :cache => true } %></p> <p><%= calendar_field ''log'', ''accesstime2'', { :class => ''date'', :field_title => ''Between'', :button_title => ''Show calendar'' }, { :ifFormat => ''%Y-%m-%d %l:%M:%S'', :firstDay => 1, :step => 1, :showOthers => true, :cache => true } %></p> <pre>2004-08-03 01:25:12</pre> <input name="submit" type="submit" value="Query" /> </form>
On 5/16/05, Ron Sweeney <ron.sweeney-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> def search > > @logs = Log.find_by_sql [''SELECT * FROM logs WHERE user LIKE ? and > accesstime > ? and accesstime < ?'', "%#{@params[''user'']}%", > "%#{@params[''accesstime'']}%", "%#{@params[''accesstime2'']}%"] > > @logs = Log.find_by_sql [''SELECT * FROM logs WHERE user LIKE ? and > BETWEEN accesstime ? and accesstime ?'', "%#{@params[''user'']}%", > "%#{@params[''accesstime'']}%", "%#{@params[''accesstime2'']}%"] > > endHave you looked at the log to view the actual SQL generated by that? My guess is that it makes something like: WHERE user LIKE ''%foo%'' and accesstime > ''%2005-05-16 08:11:00%'' and ... Notice the % around the date. I would bet this is your problem and why you are having invalid syntax problems. When in doubt, check the log. Try it without the % around the date params. Jason
yes, thank you. I hardcode the values and it works great... looks like now im just having issues passing the params to the script... my date params are blank... am I on the right track on the form part? The log shows that I am not even passing the values... by any chance do you know what I have to modify in the form to pass the calendar fields as a parameter? controller: @logs = Log.find_by_sql [''SELECT * FROM logs WHERE user LIKE ? AND accesstime < ? and accesstime > ?'', "%#{@params[''T1'']}%", "#{@params[''T2'']}", "#{@params[''T3'']}"] log: Processing LogsController#search (for 127.0.0.1 at Mon May 16 10:54:50 Eastern Daylight Time 2005) Parameters: {"action"=>"search", "log"=>{"T2"=>"2004-05-16 10:54:00", "T3"=>"2005-05-21 10:54:41"}, "B2"=>"Add item", "controller"=>"logs", "T1"=>"sweeney"} [4;33mLog Load (0.050000) [1;37mSELECT * FROM logs WHERE user LIKE ''%sweeney%'' AND accesstime < '''' and accesstime > '''' Rendering logs/search within layouts/logs [4;35mLog Columns (0.000000) [0;37mSHOW FIELDS FROM logs Rendering layouts/logs (200 OK) Completed in 0.20000 (4 reqs/sec) | Rendering: 0.02000 (9%) | DB: 0.05000 (25%) form: <form method="post" action="/logs/search"> New item: <input type="text" name="T1" size="20"> <p> <%= calendar_field ''log'', ''T2'', { :class => ''date'', :field_title => ''T2'', :button_title => ''Show calendar'' }, { :ifFormat => ''%Y-%m-%d %l:%M:%S'', :firstDay => 1, :step => 1, :showOthers => true, :cache => true } %></p> <p><%= calendar_field ''log'', ''T3'', { :class => ''date'', :field_title => ''T3'', :button_title => ''Show calendar'' }, { :ifFormat => ''%Y-%m-%d %l:%M:%S'', :firstDay => 1, :step => 1, :showOthers => true, :cache => true } %></p> <input name="B2" input type="submit" value="Add item"> </form> On 5/16/05, Jason Foreman <threeve.org-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5/16/05, Ron Sweeney <ron.sweeney-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > def search > > > > @logs = Log.find_by_sql [''SELECT * FROM logs WHERE user LIKE ? and > > accesstime > ? and accesstime < ?'', "%#{@params[''user'']}%", > > "%#{@params[''accesstime'']}%", "%#{@params[''accesstime2'']}%"] > > > > @logs = Log.find_by_sql [''SELECT * FROM logs WHERE user LIKE ? and > > BETWEEN accesstime ? and accesstime ?'', "%#{@params[''user'']}%", > > "%#{@params[''accesstime'']}%", "%#{@params[''accesstime2'']}%"] > > > > end > > Have you looked at the log to view the actual SQL generated by that? > My guess is that it makes something like: > > WHERE user LIKE ''%foo%'' and accesstime > ''%2005-05-16 08:11:00%'' and ... > > Notice the % around the date. I would bet this is your problem and > why you are having invalid syntax problems. When in doubt, check the > log. Try it without the % around the date params. > > > Jason >
On 5/16/05, Ron Sweeney <ron.sweeney-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> @logs = Log.find_by_sql [''SELECT * FROM logs WHERE user LIKE ? AND > accesstime < ? and accesstime > ?'', "%#{@params[''T1'']}%", > "#{@params[''T2'']}", "#{@params[''T3'']}"]> Parameters: {"action"=>"search", "log"=>{"T2"=>"2004-05-16 > 10:54:00", "T3"=>"2005-05-21 10:54:41"}, "B2"=>"Add item",Well, looking at the parameters in the log, You can see that the T2 and T3 are inside of another parameter ''log'', so you need @params[''log''][''T2'']. Did you name the form elements something like name="log[T2]" or something? Rails does a bit of magic on request parameters when it builds the hash. Jason
perfect! and thank you... On 5/16/05, Jason Foreman <threeve.org-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5/16/05, Ron Sweeney <ron.sweeney-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > @logs = Log.find_by_sql [''SELECT * FROM logs WHERE user LIKE ? AND > > accesstime < ? and accesstime > ?'', "%#{@params[''T1'']}%", > > "#{@params[''T2'']}", "#{@params[''T3'']}"] > > > Parameters: {"action"=>"search", "log"=>{"T2"=>"2004-05-16 > > 10:54:00", "T3"=>"2005-05-21 10:54:41"}, "B2"=>"Add item", > > Well, looking at the parameters in the log, You can see that the T2 > and T3 are inside of another parameter ''log'', so you need > @params[''log''][''T2'']. Did you name the form elements something like > name="log[T2]" or something? Rails does a bit of magic on request > parameters when it builds the hash. > > > Jason >
On 16.5.2005, at 15:47, Ron Sweeney wrote:> I am so close to getting the date picker to work, and I think my short > comings are sql based... > > The helper is great and works like a champ!I must''ve missed something, but could you point me to this calendar helper? //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails