Rails SQLException语法错误(Model.where('date>?和date无返回空列表 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)

Rails SQLException语法错误(Model.where('date>?和date无返回空列表 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,Sql,Ruby On Rails 3,Datetime,我试图查找两个约会时间之间的“事件” if (params[:timeMin].present? and params[:timeMax].present?) @events = Event.where('date > ? AND date < ?', params[:timeMin], params[:timeMax]) 我不断遇到类似的错误,我不知道如何修复它 SQLite3::SQLException: near ",": syntax error: SE

我试图查找两个约会时间之间的“事件”

    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)
您正在返回需要解析为字符串的datetime_select哈希。一种方法是执行以下操作:

min_hash = params[:timeMin]
max_hash = params[:timeMax]

min_time_string = "#{min_hash['(1i)']}-#{min_hash['(2i)']}-#{min_hash['(3i)']} #{min_hash['(4i)']}:#{min_hash['(5i)']}:#{min_hash['(6i)']}"

max_time_string = "#{max_hash['(1i)']}-#{max_hash['(2i)']}-#{max_hash['(3i)']} #{max_hash['(4i)']}:#{max_hash['(5i)']}:#{max_hash['(6i)']}"
在此解析之后,应该可以执行以下操作:

@events = Event.where('date > ? AND date < ?', min_time_string, max_time_string)

添加了timeMin和timeMax来自的表单。希望这会有所帮助。尝试在railsThanks中找出模型搜索以获得帮助,但仍然存在问题。1如果我设置了它应该选择事件的日期,它将运行并且不返回任何事件列表为空。2如果我不更改日期min==max,它将返回一个错误页面请求-URI太大。任何想法都不确定我是否遵循问题1…这两个问题的日志是什么样子的?发布日志。是否可以查看每个modelSure的当前“日期”确定。您可以将其粘贴在Rails.logger.info调用中,并将其打印到日志中。我还更新了我的答案-没有6i。我还使用了date.parse来实际执行我会生成一个合法的日期对象。试试看。