我工作的一个项目上和IM很新的轨道工作,不能得到模型提交并验证显示页面
我无法弄清楚什么是错的exectly。 我得到这个错误。
NoMethodError在产品#指数
未初始化的常量的ProductsController ::优惠
Esentially我有一个特点,我尝试推行。
在我的产品表中我有一个名为保留价格的专栏,我想要一个用户在产品页面上的表单上提交一个数字,然后验证它是否保留底价,如果接受它会被添加到购物车,如果不是闪光请输入更高的报价,
问题是我似乎无法弄清楚如何让模型和控制器协同工作。
我一直在这一周,我仍然没有线索。
我想知道是否有人可以看看我的代码,看看我缺少的视图页我得到的错误,为NilClass:类的未定义的方法`model_name',我确信我输入了正确的模型形式,如果我能得到这个工作,我可以快速完成其余的工作,但我不知道我缺少什么。
报价controller.rb 类OffersController < ApplicationController中
attr_accessible:产品,:报价,:reserve_price
DEF新 @offer = Offer.new 端
end
报价模型.rb
class Offer < ActiveRecord::Base
belongs_to的:产品 的has_many:reserve_prices
attr_accessible:产品:报价,:reserve_price
validates_presence_of:提供 验证:ensure_meets_reserve_price
私人 高清ensure_meets_reserve_price 如果量< self.product。 reserve_price errors.add(:金额,“不符合底价”) 结束 结束
私人 高清reserve_price product.reserve_price 结束
高清your_offer @your_offer = Offer.new
末
高清新 @offer =发售。新=:your_offer 端
end
产品索引视图文件
class ProductsController < ApplicationController
的before_filter:认证,:除了=> [:索引,:显示]
#GET /产品 #GET /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 /产品/ 1 #GET /products/1.xml DEF显示
@product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @product }
end
端
#GET /产品/新 #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 /产品/ 1 /编辑 DEF编辑 @product = Product.find(PARAMS [:ID]) 端
#POST /产品 #POST /产品。 XML DEF创建 @product = current_user.products.new(PARAMS [:产品])
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 /产品/ 1 #PUT /products/1.xml DEF更新 @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
端
#DELETE /产品/ 1 #DELETE /products/1.xml DEF破坏 @product = Product.find(PARAMS [:ID]) @ product.destroy
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
末 结束
产品controller.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
任何帮助吗?
很多appricated我在这一段时间,并没有想通了!
您的视图属于哪个操作? – ryudice 2012-07-25 22:27:48
该视图是一个产品索引视图,我只是粘贴form_for它的自我,因为它没有连接,这是我的第一个问题:) – user1529597 2012-07-25 22:30:17
你需要使用嵌套的资源,你可以发布你的产品控制器? – ryudice 2012-07-25 22:34:55