2012-07-25 47 views
0

我工作的一个项目上和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我在这一段时间,并没有想通了!

+0

您的视图属于哪个操作? – ryudice 2012-07-25 22:27:48

+0

该视图是一个产品索引视图,我只是粘贴form_for它的自我,因为它没有连接,这是我的第一个问题:) – user1529597 2012-07-25 22:30:17

+0

你需要使用嵌套的资源,你可以发布你的产品控制器? – ryudice 2012-07-25 22:34:55

回答

0

如果我理解正确你的问题:

  • 错误显示当访问产品#显示
  • 要包括在产品#显示页面

  • 报价形式

在这种情况下,您需要初始化产品中的@offer变量像这样sController show动作:

@offer = Offer.new 

加成

下一个问题:ProductsController类::报价是未知的,它不应该是因为你有一个报价模型。我刚刚尝试将您的优惠表单包含在展示操作中,除此之外,您还使用优惠的新实例初始化了该字段。 (也许是一个数额?)。无论如何,我无法从您的代码片段中看到为什么Offer模型在您的控制器中不可用。你能提供完整的来源吗?

我首先怀疑你的怪私有方法在发售

def your_offer 
    @your_offer = Offer.new 

    end 

def new 
    @offer = Offer.new = :your_offer 
end 

是原因,但我将他们和形式呈现的罚款。但我应该做什么?

+0

是该页面将不会与它的形式,当我更新了产品位指示我仍然得到这个NoMethodError在产品#指数显示 未定义的方法'MODEL_NAME”的NilClass: – user1529597 2012-07-25 23:25:55

+0

类对不起,我想我chaged错误之一: )我把它放在产品的展示和索引中,现在我得到一个不同的错误,所以进展:)其未初始化的常量ProductsController :: Offer – user1529597 2012-07-26 00:11:09

+0

好吧,我想这是一个新问题,那么,如果我已经回答了你的问题,如果你接受它会很好。至于新的问题:你应该做的第一件事是仔细阅读错误信息,找出错误发生的位置(文件,行号......) - 也许你自己找到了答案,如果没有,提供这个信息与问题。 – bento 2012-07-26 00:48:03