0

我正在做一个项目,我对 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

有什么帮助吗?

很高兴我已经有一段时间了,还没有弄清楚!

4

1 回答 1

0

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

  • 访问产品时显示错误#show
  • 您想在产品#show 页面中包含报价单

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

@offer = Offer.new

添加

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

我首先怀疑你在Offer中奇怪的私有方法

 def your_offer
     @your_offer = Offer.new

   end

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

是原因,但我已将它们包括在内,并且表单呈现良好。但我到底应该怎么做?

于 2012-07-25T22:59:28.683 回答