[英]How to not return anything with Ransack Search Gem?
I'm using the Ransack Gem in my Rails app. 我在Rails应用中使用Ransack宝石。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.