无法让模型在显示页面上进行提交和验证

时间:2012-07-25 22:24:21

标签: ruby-on-rails ruby ruby-on-rails-3

我正在开发一个项目,我对rails很新,

我无法弄清楚出了什么问题。 我收到了这个错误。

产品中的NoMethodError #index

未初始化的常量ProductsController :: Offer

基本上我有一个我试图实现的功能。

在我的产品表中我有一个名为底价的栏目,我想要一个用户在产品页面上的表格上提交一个号码然后再次确认它的底价,如果接受它会被添加到购物车,如果没有闪存请输入更高的报价,

问题是我似乎无法弄清楚如何使模型和控制器协同工作。

我整个星期都在这,我仍然没有线索。

我想知道是否有人可以查看我的代码,看看我缺少的视图页面我得到的错误是NilClass的未定义方法`model_name':Class和我确信我为表格输入了正确的模型如果我可以开始工作,我可以快速完成其余工作,但我不知道我错过了什么。

提供controller.rb     class OffersController< ApplicationController中

attr_accessible:product,:offer,:reserve_price

def new   @offer = Offer.new   端

end

提供model.rb

   class Offer < ActiveRecord::Base

belongs_to:产品   has_many:reserve_prices

attr_accessible:product,:offer,:reserve_price

validates_presence_of:offer   validate:ensure_meets_reserve_price

私人   def ensure_meets_reserve_price     如果金额&lt; self.product.reserve_price       errors.add(:金额,“不符合底价”)     结束   端

私人   def reserve_price      product.reserve_price   端

def your_offer     @your_offer = Offer.new

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

 end

产品索引视图文件

   class ProductsController < ApplicationController

before_filter:authenticate,:except =&gt; [:index,:show]

#GET / products   #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 / 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 /编辑   def编辑     @product = Product.find(params [:id])   端

#POST / products   #POST /products.xml   def创建     @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更新     @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 / 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

端 端

产品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

任何帮助?

很久以前,我已经有一段时间了,并且没有想到它!

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题:

  • 访问产品时显示错误#show
  • 您希望在产品#show page

    中添加优惠表单        

在这种情况下,您需要在ProductsController show动作中初始化@offer变量,如下所示:

@offer = Offer.new

ADDITION

到下一个问题:ProductsController :: Offer是未知的,它不应该是你有一个Offer模型。我刚刚尝试将您的要约表格包含在展示操作中,除了您使用新的商品实例初始化该字段外,它还可以。 (也许是相当数量?)。无论如何,我无法从您的代码片段中看到为什么您的控制器中没有Offer模型。你能提供完整的资源吗?

我首先怀疑你提供的奇怪私人方法

 def your_offer
     @your_offer = Offer.new

   end

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

成为原因,但我已将它们包括在内并且表格呈现得很好。但我到底应该怎么做?