简体   繁体   English

使用will_paginate和Rails时显示部分结果

[英]Displaying partial results when with will_paginate and Rails

I am using will_paginate for the first time. 我是第一次使用will_paginate。 My initial implementation is working great. 我最初的执行效果很好。 Something like: 就像是:

@products = Product.paginate(:page => params[:page], :per_page => 10)

In the Product controller, this produces what I was expecting, and I can flip through product listings fine. 在产品控制器中,这产生了我所期望的,我可以很好地浏览产品清单。 What I'm trying to do now is show only products that contain the word "glass", and can't figure out how to get it done. 我现在想做的是仅显示包含“玻璃”一词的产品,而无法弄清楚如何完成它。 Any ideas? 有任何想法吗?

Use a where clause like this (assuming @product.name should contain "glass"): 使用这样的where子句(假设@product.name应该包含“ glass”):

@products = Product.where('name LIKE ?', '%glass%').paginate(...)

Don't forget the paginate arguments, which I removed for clarity. 不要忘记paginate参数,为清楚起见我将其删除。

if you want to search word "glass" only from one perticular column you can use it as, 如果您只想从一个垂直列中搜索“玻璃”一词,则可以将其用作,

 Product.where("'coulumn_name' like ?","% glass %").paginate(:page => params[:page], :per_page => 10)

And if you want to search it from whole record then you can use fulltext search. 如果要从整个记录中进行搜索,则可以使用全文搜索。 check out: http://railscasts.com/episodes/370-ransack?view=asciicast and https://github.com/ernie/ransack 检出: http : //railscasts.com/episodes/370-ransack ?view= asciicasthttps://github.com/ernie/ransack

Gem Ransack provides full-text search. Gem Ransack提供全文搜索。

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

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