简体   繁体   English

Ruby on Rails:带有关键字和日期范围的搜索表单不起作用

[英]Ruby on Rails : Search form with keyword and date range does not working

this is beginner of Ruby on rails.这是 Ruby 在轨道上的初学者。 I'm adding search box in index view and want to search with keyword and date range.我在索引视图中添加搜索框,并希望使用关键字和日期范围进行搜索。 Keyword search works well, but if I contains date range, it returns nothing.关键字搜索效果很好,但如果我包含日期范围,它不会返回任何内容。 bit confused, So I post here.有点困惑,所以我在这里发帖。

  • search action that implemented in Model is like this.在 Model 中实现的搜索操作是这样的。
def self.search(search, date_from, date_to)

        if !date_from.blank? && !date_to.blank?
            @searched_private_questions = self.where(" asktime BETWEEN ? AND ? ", "#{date_from}", "#{date_to}" )
        else
            @searched_private_questions = PrivateQuestion.all
        end

        if !search.blank?
            #search with user name first
            searched_user = User.where( 'name LIKE ?', "%#{search}%" )
            if !searched_user.empty?
                @searched_private_questions = @searched_private_questions.where(user: searched_user).order(:asktime)
            else
                #if search keyword is not username, search in title and body context
                @searched_private_questions = @searched_private_questions.where('title LIKE ? OR body LIKE ?', "%#{search}%","%#{search}%").order(:asktime)
            end
        else
            @searched_private_questions = @searched_private_questions.order(:asktime)
        end


    end

search (keyword) and date_from, date_to parameters are transfered well. search(keyword) 和 date_from、date_to 参数传递的很好。 but if I insert date_from and date_to parameters, nothing returns from where clause.但是如果我插入 date_from 和 date_to 参数,则 where 子句不会返回任何内容。

Can be related with pagination code?可以与分页码相关吗?

  • in controller index action is like this:在 controller 索引动作是这样的:
def index
    @searched_private_questions = PrivateQuestion.search(params[:search], params[:date_from], params[:date_to])
    @pagy, @private_questions = pagy(@searched_private_questions, items:10) 
  end

I'm using rails 5 and bootstrap 3 with cdn, and bootstrap datepicker.我正在使用带有cdn的rails 5和bootstrap 3,以及bootstrap datepicker。 datepicker works well.日期选择器效果很好。 I wish someone help me... Thanks.我希望有人帮助我...谢谢。

Log picture: enter image description here日志图片:在此输入图片描述

I found interesting points,..;;;!!!我发现了有趣的点,..;;;!!! it works if I execute this in production environment, which connects to PostgreSQL database.如果我在生产环境中执行它,它会工作,它连接到 PostgreSQL 数据库。 Might be related to sqllite settings.可能与 sqllite 设置有关。 Anyway it works in production ENV!无论如何,它适用于生产环境! Sorry for confusing ;;;很抱歉混淆了;;;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM