Ruby on Rails中未初始化的常量错误

时间:2012-03-21 20:54:36

标签: ruby-on-rails controllers

当我尝试在我的电子商务商店中创建新产品时,你继续在rails中出现此错误,错误如下:“ProductsController#update uninitialized constant Product :: Categories”中的NameError。我基本上试图为产品分配一个类别,例如,一个名为White polo的产品将进入Tops等类别...它在我创建产品时显示包含所有类别的下拉列表但是当我点击创建时得到例外。

我的控制器代码如下:

class ProductsController < ApplicationController
skip_before_filter :authorize, only: [:show]        

# GET /products
# GET /products.json
def index
@products = Product.all

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @products }
end
end

# GET /products/1
# GET /products/1.json
def show
@product = Product.find(params[:id])
@cart = current_cart

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @product }
end
end

def preview
@product = Product.find(params[:id])

respond_to do |format|
  format.html # preview.html.erb
  format.json { render json: @product }
end
end  


# GET /products/new
# GET /products/new.json
def new
@product = Product.new
@categories = Category.all

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @product }
end
end

# GET /products/1/edit
def edit
@product = Product.find(params[:id])
@categories = Category.all
end

# POST /products
# POST /products.json
def create
@product = Product.new(params[:product])


respond_to do |format|
  if @product.save
    format.html { redirect_to products_url, 
      notice: 'Product was successfully created.' }
    format.json { render json: @product, status: :created, 
      location: @product }
  else
    format.html { render action: "new" }
    format.json { render json: @product.errors, 
      status: :unprocessable_entity }
  end
  end
  end

  # PUT /products/1
  # PUT /products/1.json
  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.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @product.errors, 
      status: :unprocessable_entity }
  end
  end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
  @product = Product.find(params[:id])
  @product.destroy

  respond_to do |format|
  format.html { redirect_to products_url }
  format.json { head :no_content }
  end
  end


  def who bought 
  @product = Product.find(params[:id])
  respond_to do |format|
  format.atom
    end
end
 end 

产品编辑视图代码:

<%= form_for(@product) do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

 <ul>
 <% @product.errors.full_messages.each do |msg| %>
 <li><%= msg %></li>
 <% end %>
 </ul>
 </div>
 <% end %>

 <div class="field">
 <%= f.label :name %><br />
 <%= f.text_field :name %>
 </div>

 <div class="field">
 <%= f.collection_select :categories, Category.all, :category_id, :category_name, :include_blank =>'None' %><br />
 </div> 

<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description, rows: 6 %>
</div>

<div class="field">
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</div>
<div class="field">
<%= f.label :image_url1 %><br />
<%= f.text_field :image_url1 %>
</div>
<div class="field">
<%= f.label :image_url2 %><br />
<%= f.text_field :image_url2 %>
</div>
<div class="field">
<%= f.label :image_url3 %><br />
<%= f.text_field :image_url3 %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :quantity %><br />
<%= f.text_field :quantity %>
</div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>

我可能做错了什么?

2 个答案:

答案 0 :(得分:1)

如果您未在各自的模型中声明产品和类别之间的关联(在产品中,添加belongs_to :category,在类别中添加has_many :products),就会发生这种情况。您还需要确保产品数据库表具有列category_id

答案 1 :(得分:0)

我现在已修复它,发生的事情是我创建了一个category_id列,因为它们不需要因为rails已经创建了一个名为id的所以当我尝试创建一个新产品时它会产生不匹配并将它们还给我例外。使用rails控制台非常有用,因为我深挖并发现了类别表中的实际内容。