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

时间:2012-09-25 01:35:14

标签: ruby-on-rails ruby-on-rails-3 railscasts

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

<% form_tag purchases_path, :method => 'get', :id => "products_search" do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
<% end %>

<% form_tag sales_path, :method => 'get', :id => "sales_search" do %>
      <p>
        <%= text_field_tag :search, params[:search] %>
        <%= submit_tag "Search", :name => nil %>
      </p>
    <% end %>

然后在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.collect{ |m| m if m.kind == "purchase"}.compact
    @sales=@microposts.collect{ |m| m if m.kind == "sale"}.compact

  end

编辑:

我也尝试过使用

def home

    @microposts=Micropost.all
    @purchases=@microposts.purchases
    @sales=@microposts.sales

  end

相反,但它给了我错误的未定义方法`购买'#

Anways,

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

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

5 个答案:

答案 0 :(得分:0)

def home
  @microposts = Micropost.all
  @purchases = Micropost.search("purchase", params[:search])
  @sales = Micropost.search("sale", params[:search])
end

def self.search(kind = nil, search = nil)
  results = self.where(:kind => kind) if kind
  results = self.where('name LIKE ?', "%#{search}%") if name
end

答案 1 :(得分:0)

好的,所以你有一个sales_path和purchases_path,所以我假设它们都会导致一个带有销售和购买行为的控制器

def sales
  @search = params[:search]
  @microposts = Micropost.search(@search).sales
end

def purchases
  @search = params[:search]
  @microposts = Micropost.search(@search).purchases
end

Micropost.search方法将返回范围。因此,我们将在该范围内调用#sales或#purhases,这将在查询中附加条件。

更新

看起来你想从1种形式中搜索两种不同类型的微博。

应用程序/视图/家/ index.html.erb

<%= form_tag(root_path, :method => :get) do
 <%= text_field_tag 'search'%>
 <%= submit_tag 'Search' %>
%>

应用程序/模型/ micropost.rb

scope :purchase_or_sale, where("kind IN ?", ["purchase", "sale"])
def self.search(search)
  if search
    where('name LIKE ?', "%#{search}%")
  else
    scoped
  end
end

def purchase?
  kind == "purchase"
end

def sale?
  kind == "sale"
end

/app/controllers/home_controller.rb

def home
  @microposts = Micropost.search(params[:search]).purchase_or_sale
  @purchases = @microposts.select(&:purchase?)
  @sales = @microposts.select(&:sale?)  
end

更新2

好的,还有一个更新....这就是我现在想要的东西:2个搜索表单,两个/微博......如果我在销售表单上搜索,它会显示销售情况。如果我在购买表单上搜索,它将搜索购买。

你的观点:

<%= form_tag(microposts_path(kind: "purchases", :method => :get) do
 <label for="search">Search Purchases:</label>
 <%= text_field_tag 'search' %>
 <%= submit_tag 'Search' %>
%>

<%= form_tag(microposts_path(kind: "sales", :method => :get) do
 <label for="search">Search Sales:</label>
 <%= text_field_tag 'search' %>
 <%= submit_tag 'Search' %>
%>

你的控制器:

def index
  @microposts = Micropost.search(params[:search).for_kind(params[:kind])
end

你的模特

def self.search(search_text)
  if search_text
    where('name LIKE ?', "%#{search_text}%")
  else
    scoped
  end
end

scope :for_kind, lambda{|search_kind| where(kind: search_kind) }

答案 2 :(得分:0)

如果maped到MictropostsController #home,假设假设'/ microposts / home' 应用/视图/微柱/ home.html.erb

<%= form_tag('/microposts/home', :method => :get) do
 <%= text_field_tag 'kind'%>
 <%= submit_tag 'Search' %>
%>

<强> /app/controllers/microposts_controller.rb

def home
  kind = params[:kind]
  if kind.present?
    # if search parameter passed
    @microposts = Micropost.where(:kind => kind)
  else
    # if search parameter not passed, list all micro post
    @microposts = Micropost.all
  end
end

答案 3 :(得分:0)

根据您对其他回复的评论:

def home
  @microposts = Micropost.scoped # so you can later apply any scope
  if params[:purchases_search].present?
    @microposts = @microposts.purchases.search(params[:purchases_search])
  elsif params[:sales_search].present?
    @microposts = @microposts.sales.search(params[:sales_search])
  end
end

这假设两个搜索字段的名称是“purchases_search”和“sales_search”。

答案 4 :(得分:0)

为什么不在继承微博模型的两个新模型中拆分微博?

class Micropost < ActiveRecord::Base

  #do your general micropost magic here

end

下一步:

class Purchase < Micropost

  #do your purchase specific magic here

end

class Sale < Micropost

  #do your sale specific magic here

end

您的家庭行动:

def home

  @microposts = Micropost.all
  @purchases = Purchase.all
  @sales = Sale.all

end

如果您需要搜索购买:

Purchase.where(:something => "great")

如果您需要搜索销售:

Sale.where(:something => "greater")

如果您需要同时搜索:

Micropost.where(:something => "the greatest")

**注意**您需要在微博表中添加一个“类型”列才能使其生效,更多信息:单表继承@ http://api.rubyonrails.org/classes/ActiveRecord/Base.html