简体   繁体   中英

How to not return anything with Ransack Search Gem?

I'm using the Ransack Gem in my Rails app. When I do a blank search it returns all posts.

How can I limit it to return nothing if blank?

I tried in my view:

<% if @q.blank? %>

,but still get all posts.

In the controller:

 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

Thanks

You can do something like this:

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

  @posts = @q.result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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