0

现在在 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

相反,但随后它给了我错误未定义的方法“购买”#

无论如何,

现在使用 .collect 方法时,我收到一个错误,提示未定义的局部变量或方法“purchases_path”,它对 sales_path 也是如此。

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

4

5 回答 5

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”。

于 2012-09-28T16:20:34.573 回答
0

假设假设 '/microposts/home' 如果映射到 MictropostsController#home app/views/microposts/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
于 2012-09-27T04:50:35.430 回答
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")

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

于 2012-10-02T14:07:19.520 回答
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
于 2012-09-25T03:25:12.590 回答
0

好的,所以你有一个 sales_path 和一个 purchase_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 个表单中搜索两种不同类型的微博。

应用程序/views/home/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 个搜索表单,都是 /microposts......如果我在销售表单上搜索,它将显示销售。如果我在购买表格上搜索,它将搜索购买。

你的看法:

<%= 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) }
于 2012-09-27T02:30:53.823 回答