未初始化的常量ProductsController ::提供错误?

时间:2012-07-26 16:37:52

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

我有一个问题,我无法弄清楚产品控制器错误是什么问题, 我不会渲染我想要工作的产品索引视图页面。

我的代码如下: 提供控制器

class OffersController < ApplicationController

  attr_accessible :product , :reserve_price


  def your_offer
    @your_offer = Offer.new

  end

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


end

和产品控制器

class ProductsController < ApplicationController
      before_filter :authenticate, :except => [: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
      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

提供型号

class Offer < ActiveRecord::Base
     belongs_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

  private
  def reserve_price
     product.reserve_price
  end



  def your_offer
    @your_offer = Offer.new

  end

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

产品索引viex代码段

<%= form_for @offer do |f| %>
    <%= f.text_field :your_offer %>
    <%= f.submit "Make Offer" %>
<% end %>

有人能看到我的恐怖在哪里吗?

1 个答案:

答案 0 :(得分:0)

抱怨@offer = Offer.new

您是否在创建优惠后运行迁移并重新启动服务器?

您是否将其声明为config / routes.rb中的资源

  resources :products, :shallow => true do
    resources :offers # or at least just this line
  end

修改

摆脱这一行,然后再试一次

attr_accessible :product, :offer , :reserve_price

是:在商品表中提供一个列?

您不能在attr_accessible中拥有其他模型的列。