繁体   English   中英

Ransack Search Gem如何不退货?

[英]How to not return anything with Ransack Search Gem?

我在Rails应用中使用Ransack宝石。 当我进行空白搜索时,它将返回所有帖子。

如果空白,我如何限制它不返回任何内容?

我认为:

<% if @q.blank? %>

,但仍会获得所有帖子。

在控制器中:

 def index

    @q = Post.search(params[:q])
    @posts = @q.result(:distinct => true)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

谢谢

您可以执行以下操作:

  if !params[:q].blank?
    @q=Post.search(params[:q])
  else
    @q=Post.search({:id_eq => 0})
  end

  @posts = @q.result

暂无
暂无

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

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