我正在做一个项目,我对 Rails 很陌生,
我无法弄清楚到底出了什么问题。我得到这个错误。
产品中的 NoMethodError#index
未初始化的常量 ProductsController::Offer
本质上,我有一个正在尝试实现的功能。
在我的产品表中,我有一个名为底价的列,我希望用户在产品页面上的表格上提交一个数字,然后验证它与底价,如果接受它会添加到购物车,如果不闪请输入更高的报价,
问题是我似乎无法弄清楚如何让模型和控制器协同工作。
我整个星期都在这,我仍然不知道。
我想知道是否有人可以查看我的代码并查看我在视图页面中缺少什么可以让它工作我可以快速完成其余的工作,但我不知道我错过了什么。
提供控制器.rb 类 OffersController < ApplicationController
attr_accessible :product, :offer , :reserve_price
def new @offer = Offer.new 结束
end
报价模型.rb
class Offer < ActiveRecord::Base
属于_to :product has_many :reserve_prices
attr_accessible :product, :offer , :reserve_price
validates_presence_of :offer validate :ensure_meets_reserve_price
private def ensure_meets_reserve_price if amount < self.product.reserve_price errors.add(:amount, "does not meet reserve price") end end
私人 def reserve_price product.reserve_price end
def your_offer @your_offer = Offer.new
结尾
def new @offer = Offer.new = :your_offer end
end
产品索引视图文件
class ProductsController < ApplicationController
before_filter :authenticate, :except => [:index, :show]
# 获取 /products # 获取 /products.xml
def index @offer = Offer.new
@products = Product.search(params[:search_query])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
end
结尾
# GET /products/1 # GET /products/1.xml def show
@product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @product }
end
结尾
# GET /products/new # GET /products/new.xml def new @product = Product.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @product }
end
结尾
# GET /products/1/edit def edit @product = Product.find(params[:id]) end
# POST /products # POST /products.xml def create @product = current_user.products.new(params[:product])
respond_to do |format|
if @product.save
format.html { redirect_to(@product, :notice => 'Product was successfully created.') }
format.xml { render :xml => @product, :status => :created, :location => @product }
else
format.html { render :action => "new" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
结尾
# PUT /products/1 # PUT /products/1.xml def update @product = Product.find(params[:id])
respond_to do |format|
if @product.update_attributes(params[:product])
format.html { redirect_to(@product, :notice => 'Product was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
结尾
# 删除 /products/1 # 删除 /products/1.xml def destroy @product = Product.find(params[:id]) @product.destroy
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
结束结束
产品控制器.rb
class ProductsController < ApplicationController
before_filter :authenticate, :except => [:index, :show]
# GET /products
# GET /products.xml
def index
@products = Product.search(params[:search_query])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
end
end
# GET /products/1
# GET /products/1.xml
def show
@product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @product }
end
end
# GET /products/new
# GET /products/new.xml
def new
@product = Product.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @product }
end
end
# GET /products/1/edit
def edit
@product = Product.find(params[:id])
end
# POST /products
# POST /products.xml
def create
@product = current_user.products.new(params[:product])
respond_to do |format|
if @product.save
format.html { redirect_to(@product, :notice => 'Product was successfully created.') }
format.xml { render :xml => @product, :status => :created, :location => @product }
else
format.html { render :action => "new" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
# PUT /products/1
# PUT /products/1.xml
def update
@product = Product.find(params[:id])
respond_to do |format|
if @product.update_attributes(params[:product])
format.html { redirect_to(@product, :notice => 'Product was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.xml
def destroy
@product = Product.find(params[:id])
@product.destroy
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
end
end
有什么帮助吗?
很高兴我已经有一段时间了,还没有弄清楚!