简体   繁体   English

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

[英]Rails error “NoMethodError” - Just started to use Rails

Hopefully this is a very easy problem to solve as I am just starting out with Rails. 希望这是一个非常容易解决的问题,因为我刚开始使用Rails。

I have one database (Products) which contains a number of product attributes including brand, colour and product_type. 我有一个数据库(Products),其中包含许多产品属性,包括品牌,颜色和product_type。

I currently have an index page (within productfinder directory) which is able to provide a summary of the products in the database. 我目前有一个索引页面(位于productfinder目录中),该页面可以提供数据库中产品的摘要。 I am using the following code in index.html.erb: 我在index.html.erb中使用以下代码:

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

Within productfinder_controller.rb i have: 在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

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". 现在,我想在索引页面上添加3个按钮,这些按钮将使用户能够重新计算产品子集的独特品牌和独特颜色-匹配product_type =“ new”或“ used”。 The third button would correspond to the results not being filtered at all (ie as the index page loads initially). 第三个按钮将对应于根本不被过滤的结果(即,索引页面最初加载时)。

In a previous question I was advised to add the following code: In productfinder_controller: 在上一个问题中,建议我添加以下代码:在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: 在Product.rb中:

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

In index.html.erb 在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| %> 现在运行应用程序时,出现NoMethodError-ProductFinder#Index(尝试执行nil.each时,该错误与<%@ distinctbrands.each相关| 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: 我想知道问题是否出在定义@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

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? 问题是否与在def索引中未定义@products有关? If so, how can this be done? 如果是这样,该怎么办?

Many thanks for your time 非常感谢您的宝贵时间

Using the before_filter, you're creating an instance of @products, but your index is still loading @distinctbrands. 使用before_filter,您正在创建@products的实例,但是索引仍在加载@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. 在before_filter和模型中,添加用于将产品添加到该变量的所有逻辑。 Use scopes to make the queries chain-able (using Rails 3?) 使用范围使查询可链接(使用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. 在load_products方法中使用条件来搜索params哈希值,以确定是否应通过params中设置的选项或仅全部加载产品。

So in your model, the load_products method could look like: 因此,在您的模型中,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

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? 如果按类型过滤,颜色何时起作用?

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: 感谢您的所有帮助-我再次听了您的建议,并设法确定product.rb中有一个小错误,现在显示为:

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

All seems to be working well! 一切似乎都运作良好! Thanks again 再次感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM