2013-06-24 44 views
0

我在我的Rails應用程序中使用了Ransack Gem。當我做一個空白搜索時,它會返回所有帖子。如何使用Ransack Search 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 

感謝

回答

0

你可以做這樣的事情:

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

    @posts = @q.result