0

希望这是一个非常容易解决的问题,因为我刚开始使用 Rails。

我有一个数据库(产品),其中包含许多产品属性,包括品牌、颜色和产品类型。

我目前有一个索引页面(在 productfinder 目录中),它能够提供数据库中产品的摘要。我在 index.html.erb 中使用以下代码:

<% @distinctbrands.each do |distinctbrand| %>
  <h4><%= distinctbrand %></h4>
<% end %>

在 productfinder_controller.rb 我有:

 def index
    @distinctbrands = Product.find( :all, :order => 'brand ASC', :select => 'brand' ).map{ |i| i.brand }.uniq
    @distinctcolours = Product.find( :all, :order => 'colour ASC', :select => 'colour' ).map{ |i| i.colour }.uniq
  end

到目前为止,一切似乎都在正常工作

现在我想在我的索引页面上添加 3 个按钮,这些按钮将使用户能够重新计算产品子集的 distinctbrands 和 distinctcolours - 匹配 product_type = "new" 或 "used"。第三个按钮将对应于根本没有被过滤的结果(即最初加载索引页面时)。

在上一个问题中,我被建议添加以下代码:在 productfinder_controller 中:

before_filter :load_products, :only => [:index, :bybrand, :bycolour, :byprice]
protected

 def load_products
    @products = Product.by_product_type(params[:product_type])
  end

在 Product.rb 中:

scope :by_product_type, lambda{  |product_type| where(product_type: product_type.to_i) unless product_type.nil? }

在 index.html.erb 中

<%= link_to "New", :controller => params[:controller], :action => params[:action], :product_type => 'New' %>
<%= link_to "Used", :controller => params[:controller], :action => params[:action], :product_type => 'Used' %>
<%= link_to "Don't Restrict", :controller => params[:controller], :action => params[:action], :product_type => '' %>

现在运行应用程序时,我得到一个 NoMethodError - ProductFinder#Index(尝试执行 nil.each 时,该错误与 <% @distinctbrands.each do |distinctbrand| %> 相关联

我想知道问题是否出在定义 @distinctbrands 和 @distinctcolours 的函数上,因为现在已经过滤了数据库,但是对于以下两种情况,我都会遇到相同的错误:

def index
  @distinctbrands = @product.find( :all, :order => 'brand ASC', :select => 'brand' ).map{ |i| i.brand }.uniq
  @distinctcolours = @product.find( :all, :order => 'colour ASC', :select => 'colour' ).map{ |i| i.colour }.uniq
end

def index
  @distinctbrands = Product.find( :all, :order => 'brand ASC', :select => 'brand' ).map{ |i| i.brand }.uniq
  @distinctcolours = Product.find( :all, :order => 'colour ASC', :select => 'colour' ).map{ |i| i.colour }.uniq
end

请问有人可以为我提供一个可能的解决方案吗?问题是否与 @products 未在 def 索引中定义有关?如果是这样,如何做到这一点?

非常感谢您的时间

4

2 回答 2

1

使用 before_filter,您正在创建@products 的实例,但您的索引仍在加载@distinctbrands。

我认为你应该做的是:

  1. 坚持使用一个实例变量来加载产品。
  2. 在 before_filter 和模型中添加用于将产品添加到该变量的所有逻辑。使用范围使查询可链接(使用 Rails 3?)
  3. 使用 load_products 方法中的条件搜索参数哈希来确定产品是否应该由参数中设置的选项加载,或者全部加载。

因此,在您的模型中, load_products 方法可能如下所示:

def load_products
  if params[:product_type]
    @products = Product.by_product_type(params[:product_type])
  else
    @products = Product.find( :all, :order => 'brand ASC', :select => 'brand' ).map{ |i| i.brand }.uniq
  end
end

然后在您的索引中更新为:

<% @products.each do |product| %>
  <h4><%= product.name %></h4>
<% end %>

我可能会感到困惑,但我在这里看到了一些不同的东西。我看到品牌、颜色和类型。您要显示的默认视图是什么?如果它们按类型过滤,颜色什么时候起作用?

于 2011-02-15T01:35:15.323 回答
0

感谢您的所有帮助 - 我再次查看了您的建议并设法确定我在 product.rb 中有一个小错误,现在它显示为:

scope :by_product_type, lambda{|product_type| where(:product_type => product_type) unless product_type.nil? }

一切似乎都运行良好!再次感谢

于 2011-02-15T10:34:44.410 回答