RoR:我如何只搜索具有特定属性的微博?

现在在app / views / microposts / home.html.erb我有..

 'get', :id => "products_search" do %> 

nil %>

'get', :id => "sales_search" do %>

nil %>

然后在micropost.rb我有

 scope :purchases, where(:kind => "purchase") scope :sales, where(:kind => "sale") def self.search(search) if search where('name LIKE ?', "%#{search}%") else scoped end end 

然后终于在microposts_controller.rb中了

 def home @microposts=Micropost.all @purchases=@microposts.purchases @sales=@microposts.sales end 

现在我收到一个错误,说明未定义的局部变量或方法`purchases_path’,它对sales_path也是如此。

我想要做的是只搜索一些微博而不是全部微博。 在我的微博表中,我有一个名为kind的列,可以是“购买”或“销售”。 如何更改这三段代码,以便一个搜索搜索并仅显示具有“购买”类型的微博的结果。 然后另一个搜索并显示那些具有“销售”类型的微博的结果

这个问题(在另一篇文章中)有一个在RoR有50个代表的赏金:我怎么才能搜索具有特定属性的微博?

你可以试试这个。

你的型号:

 class Micropost # ... scope :purchase_only, where(:kind => "purchase") # ... def self.search(search) if search self.purchase_only.find(:all, :conditions => ['name LIKE ?', "%#{search}%"]) else self.purchase_only end end end 

但是,这些东西对我来说很奇怪。

例如:你应该删除.find(...) ,这个查找程序将不推荐使用Rails 4。