【发布时间】:2014-01-14 01:40:16
【问题描述】:
我正在尝试查找两个日期时间之间的“事件”。
if (params[:timeMin].present? and params[:timeMax].present?)
@events = Event.where('date > ? AND date < ?', params[:timeMin], params[:timeMax])
我不断收到类似的错误,我不知道如何解决它。
SQLite3::SQLException: near ",": syntax error: SELECT "events".* FROM "events" WHERE (date > '---
- (1i)
- ''2014''
','---
- (2i)
- ''1''
','---
- (3i)
- ''8''
','---
- (4i)
- ''14''
','---
- (5i)
- ''36''
' AND date < '---
- (1i)
- ''2014''
','---
- (2i)
- ''1''
','---
- (3i)
- ''13''
','---
- (4i)
- ''14''
','---
- (5i)
- ''36''
') ORDER BY events.created_at DESC
这是导致视图中问题的行
<% @events.each do |event| %>
谢谢
要求的信息
index.html.erb
<%= form_tag events_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= datetime_select :timeMin, params[:timeMin] %>
<%= datetime_select :timeMax, params[:timeMax] %>
<%= submit_tag "Search Near", :name => nil %>
</p>
<% end %>
使用来自@CDub 的解决方案
在发出请求时记录应该返回 2 个(所有)事件的日期 -> 没有返回(空列表)
Started GET "/events?utf8=%E2%9C%93&search=&timeMin%5B%281i%29%5D=2014&timeMin%5B%282i%29%5D=1&timeMin%5B%283i%29%5D=7&timeMin%5B%284i%29%5D=15&timeMin%5B%285i%29%5D=13&timeMax%5B%281i%29%5D=2014&timeMax%5B%282i%29%5D=1&timeMax%5B%283i%29%5D=13&timeMax%5B%284i%29%5D=15&timeMax%5B%285i%29%5D=13" for 127.0.0.1 at 2014-01-13 18:13:45 -0800
Processing by EventsController#index as HTML
Parameters: {"utf8"=>"✓", "search"=>"", "timeMin"=>{"(1i)"=>"2014", "(2i)"=>"1", "(3i)"=>"7", "(4i)"=>"15", "(5i)"=>"13"}, "timeMax"=>{"(1i)"=>"2014", "(2i)"=>"1", "(3i)"=>"13", "(4i)"=>"15", "(5i)"=>"13"}}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'BVjMjs3hJ1HV6udJzEg4-g' LIMIT 1
Event Load (0.1ms) SELECT "events".* FROM "events" WHERE (date > '2014-1-7 15:13:' AND date < '2014-1-13 15:13:') ORDER BY events.created_at DESC
Rendered events/index.html.erb within layouts/application (9.1ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (1.1ms)
Rendered layouts/_footer.html.erb (0.2ms)
Completed 200 OK in 53ms (Views: 52.1ms | ActiveRecord: 0.3ms)
【问题讨论】:
标签: sql ruby-on-rails-3 datetime