【问题标题】:Rails error “NoMethodError” - Just started to use RailsRails 错误“NoMethodError” - 刚开始使用 Rails
【发布时间】:2011-02-15 00:05:46
【问题描述】:

希望这是一个非常容易解决的问题,因为我刚开始使用 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 和 @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 索引中定义有关?如果是这样,如何做到这一点?

非常感谢您的宝贵时间

【问题讨论】:

  • 数据库没有被过滤,每个查询都是独立的。当您将 @distinctbrands 查询替换为以下内容时会发生什么:@distinctbrands = ['a', 'b']。你仍然遇到同样的错误吗?

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


【解决方案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 %>

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

【讨论】:

  • 在我看来,OP 希望能够在同一页面上并作为单独的查询查看品牌、颜色和产品。
  • 感谢你们两位的cmets,很抱歉没有尽可能清楚地说明情况。通过在 'def load_grips' 中使用 if 语句,索引页面最初会加载其中的所有数据(正如我所希望的那样!)。但是,当我单击 3 个按钮中的任何一个时,产品就会消失(即我假设它们都被排除在外)。
  • 除此之外,我目前对如何在控制器中使用函数感到困惑。似乎索引页面和其他页面都可以使用在“默认加载夹点”中计算的参数?但是,'def index' 中的那些参数只能在索引页面上使用 - 这是正确的吗?其次,当使用 '.find' 函数时,这是否需要作用于 Product(即它不能作用于实例变量?)。
  • 最后,回答您关于索引页面信息的问题,目标是... 最初加载包含所有产品的页面 - 该页面应列出所有库存品牌和所有颜色库存。单击“新建”按钮时,我希望刷新这两个列表 - 即提供为“新”产品库存的所有品牌以及“新”产品的所有颜色的列表。同样,“Used”按钮应该刷新列表并显示那些刚刚匹配“Used”标准的信息。 “不限制”应该让页面恢复原来的样子。谢谢
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    相关资源
    最近更新 更多