RoR:如何仅搜索具有特定属性的微帖子



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

然后在微邮局中,我有

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 的列,可以是"购买"或"销售"。如何更改我的代码,以便一个搜索表单仅搜索并显示那些具有"购买"类型的微帖子的结果。然后另一个搜索并显示那些带有"sale"

假设假设

'/microposts/home'如果映射到MictropostsController#homeapp/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

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

app/views/home/index.html.erb

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

app/models/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) }

根据您对另一个回复的评论:

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

你为什么不把微帖子分成两个继承微帖子模型的新模型呢?

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")
**

注意 ** 您需要在微帖子表中添加一列"type"才能正常工作,更多信息:单表继承@http://api.rubyonrails.org/classes/ActiveRecord/Base.html

相关内容

  • 没有找到相关文章

最新更新