Rails 错误 “NoMethodError” - 刚刚开始使用Rails

发布于 2024-10-17 13:03:07 字数 2498 浏览 3 评论 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个按钮,这些按钮将使用户能够重新计算产品子集的不同品牌和不同颜色 - 匹配product_type =“新”或“二手”。第三个按钮将对应于根本没有被过滤的结果(即,当索引页面最初加载时)。

在上一个问题中,建议我添加以下代码: 在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 索引中定义有关?如果是这样,该怎么办?

非常感谢您抽出时间

Hopefully this is a very easy problem to solve as I am just starting out with Rails.

I have one database (Products) which contains a number of product attributes including brand, colour and product_type.

I currently have an index page (within productfinder directory) which is able to provide a summary of the products in the database. I am using the following code in index.html.erb:

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

Within productfinder_controller.rb i have:

 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

UP TO THIS POINT EVERYTHING SEEMS TO BE WORKING CORRECTLY

Now I would like to add 3 buttons onto my index page, these buttons would enable the user to recalculate the distinctbrands and distinctcolours for a subset of the products - matching either product_type = "new" or "used". The third button would correspond to the results not being filtered at all (i.e. as the index page loads initially).

In a previous question I was advised to add the following code:
In productfinder_controller:

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

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

In Product.rb:

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

In 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 => '' %>

When the application is now run I get a NoMethodError - ProductFinder#Index (while trying to do nil.each
The error is associated with <% @distinctbrands.each do |distinctbrand| %>

I wondered if the problem was with the functions defining @distinctbrands and @distinctcolours as the database has now been filtered, however I get the same error for both the following situations:

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

and

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

Is somebody able to provide me with a possible solution please? Is the problem to do with @products not being defined within def index? If so, how can this be done?

Many thanks for your time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏璃陌 2024-10-24 13:03:07

使用 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 %>

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

Using the before_filter, you're creating an instance of @products, but your index is still loading @distinctbrands.

What I think you should do is:

  1. Stick to one instance variable to load the
    products.
  2. Add all the logic for
    adding the products to that variable
    in the before_filter and in your model. Use scopes to make the queries chain-able (using Rails 3?)
  3. Use a
    condition in the load_products method searching for a params
    hash to determine if the products
    should be loaded by the option set in the params,
    or just all.

So in your model, the load_products method could look like:

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

Then in your index update to:

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

I might be confused but I see a few different things here. I see brand, color, and type. What is the default view you want show? If they are filtered by type, when does color come into play?

生死何惧 2024-10-24 13:03:07

感谢您的帮助 - 我再次查看了您的建议,并设法发现我在 Product.rb 中存在一个小错误,现在它显示:

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

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

Thanks for all your help - I went through your suggestions again and managed to identify that I had a minor error in product.rb, now it reads:

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

All seems to be working well!
Thanks again

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文