Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails上创建搜索日期字段以查找数据库中的项目_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何在Rails上创建搜索日期字段以查找数据库中的项目

Ruby on rails 如何在Rails上创建搜索日期字段以查找数据库中的项目,ruby-on-rails,Ruby On Rails,我目前有: def index @sheets = Sheet.where(user_id: current_user) end 我希望它能做到: if params[:search] @sheets = Sheet.search(params[:search]).order("created_at DESC") else @sheets = Sheet.all.order("created_at DESC") end 但是我希望视图中的字段是一个带有下拉日

我目前有:

def index
     @sheets = Sheet.where(user_id: current_user)
end
我希望它能做到:

 if params[:search]
    @sheets = Sheet.search(params[:search]).order("created_at DESC")
  else
    @sheets = Sheet.all.order("created_at DESC")
 end

但是我希望视图中的字段是一个带有下拉日历的日期字段,以便用户更容易搜索工作表,我该怎么做?我已经看过了:

参数[:search]是单一日期吗? 用于将日期字符串解析为实际日期

如果是这样,只需稍加修改即可:

Sheets.where(created_at: Time.zone.parse(params[:search])) 
但这实际上只会返回在与输入的时间完全相同的时间(到秒)创建的工作表。那么,它真的是
params[:start\u date]
&
params[:end\u date]
?至少,您可以返回在某个日期范围内创建的所有图纸:

Sheets.where( created_at: Time.zone.parse(params[:start_date])..Time.zone.parse(params[:end_date]) )

search
功能不存在,您必须使用
where
clausewold Sheets.where(创建于::search)工作