我寫了一個控制器和模型來從數據庫中獲取數據。但它顯示了我們運行rails時的錯誤:NameError在ProductsController#index中的url ..我給我的url作爲localhost:3000/products。那麼它是給錯誤爲: 資源:In rails錯誤是因爲NameError在ProductsController#index
NameError in ProductsController#index
uninitialized constant ProductsController::Product.
my controller is:
products_controller.rb
class ProductsController < ApplicationController
# GET /products
# GET /products.json
def index
@products = Product.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
end
end
#render :text => params and return false
# GET /products/PR1
# GET /products/PR1.json
def show
@product = Product.find(params[:prod_id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @product }
end
end
end
model as product.rb
class Product < ActiveRecord::Base
attr_accessible :model_name, :brand_name, :price, :discount, :qty_available
end
我在routes.rb中所申報的產品
和我設計index.html.erb和show.html.erb
我不知道哪裏出錯真的...
你有在app/models /目錄中名爲product.rb的模型? –
它是'product.rb',而不是'products.rb'。 – halfelf
frnds ..都是右..我把模型命名爲products.rb,但它應該是product.rb ..但現在錯誤是模板缺失..那是什麼? –