簡體   English   中英

Rails錯誤“ NoMethodError”-剛開始使用Rails

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

希望這是一個非常容易解決的問題,因為我剛開始使用Rails。

我有一個數據庫(Products),其中包含許多產品屬性,包括品牌,顏色和product_type。

我目前有一個索引頁面(位於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 =“ 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相關| 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

有人可以為我提供可能的解決方案嗎? 問題是否與在def索引中未定義@products有關? 如果是這樣,該怎么辦?

非常感謝您的寶貴時間

使用before_filter,您正在創建@products的實例,但是索引仍在加載@distinctbrands。

我認為您應該做的是:

  1. 堅持使用一個實例變量來加載產品。
  2. 在before_filter和模型中,添加用於將產品添加到該變量的所有邏輯。 使用范圍使查詢可鏈接(使用Rails 3?)
  3. 在load_products方法中使用條件來搜索params哈希值,以確定是否應通過params中設置的選項或僅全部加載產品。

因此,在您的模型中,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 %>

我可能會感到困惑,但是我在這里看到了一些不同的東西。 我看到品牌,顏色和類型。 您要顯示的默認視圖是什么? 如果按類型過濾,顏色何時起作用?

感謝您的所有幫助-我再次聽了您的建議,並設法確定product.rb中有一個小錯誤,現在顯示為:

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

一切似乎都運作良好! 再次感謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM