0

我在我的 Rails 应用程序中使用 Ransack Gem。当我进行空白搜索时,它会返回所有帖子。

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

我在我看来尝试过:

<% 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

谢谢

4

1 回答 1

0

你可以这样做:

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

  @posts = @q.result
于 2013-07-16T14:57:44.293 回答