新手问题 unknown attribute: cart_id 错误

limkurn · November 01, 2012 · Last by limkurn replied at November 01, 2012 · 5245 hits
model :

cart.rb  
   class Cart < ActiveRecord::Base
   attr_accessible :title, :body

    has_many :line_items, dependent: :destroy
   end

product.rb
 class Product < ActiveRecord::Base

  default_scope :order => 'title'


  has_many :line_items 
  before_destroy :ensure_not_referenced_by_any_line_item
  attr_accessible :description, :image_url, :price, :title


  validates :title, :description, :image_url,:presence => true
  validates :price,:numericality => {:greater_than_or_equal_to => 0.01}
  validates :title,:uniqueness => true
  validates :image_url,:format => {
    :with  =>%r{\.(gif|jpg|png)$}i,
     :message =>'must be a url for GIF ,JPG OR PNG image_url'}

private 

    def ensure_not_referenced_by_any_line_item
       if line_items.empty?
         return true
       else 
        errors.add(:base ,'line items present')
        return false          
        end    
    end


line_item.rb
 class LineItem < ActiveRecord::Base
  attr_accessible  :cart_id, :product_id,:quantity


  belongs_to :Product
  belongs_to :cart
end



controller

 line_item_controller.rb 中的creat方法
def create
   @cart = current_cart
   product=Product.find(params[:product_id])
    @line_item = @cart.line_items.build                     (这儿是第45)
    @line_item.product = product

   respond_to do |format|
    if @line_item.save
      format.html  { redirect_to(@line_item.cart,
        :notice =>  'line item was successfully created.')}
      format.xml {render :xml => @line_item,
        :status => :created ,:location => @line_item}
    else
      format.html {render :action => "new"}
      format.xml {render :xml => @line_item.errors,
        :status => :unprocessable_entity}
      end
    end
  end

运行是出现 ActiveRecord::UnknownAttributeError in LineItemsController#create

unknown attribute: cart_id app/controllers/line_items_controller.rb:45:in `create'

我 google 了下 还是每搞懂哪儿错了。

加个高亮吧,实在看不下去

#1 楼 @lidashuang 发帖的里面 没看见文本编辑工具 不会加

@limkurn model 里面 attr_accessible :cart_id, :product_id,:quantity 这句应该是不用写的,:cart_id 和:product_id 在你做完 has_many 和 belongs_to 以后会自动加入,:quantity 在你 migrate 数据库的时候自动加入

#3 楼 @richard020389 恩 注释了 还是一样的出现那个错误

你的 current_cart 是什么?存到数据库中了么?

#5 楼 @davidqhr class ApplicationController < ActionController::Base protect_from_forgery

private def current_cart Cart.find(session[:cart_id]) rescue ActiveRecord::RecordNotFound cart=Cart.create session[:cart_id] =cart.id
cart
end en 存没存数据库我不懂 我就 rake db:migrate 了下

能看看你 lineitem 的 migration 文件怎么写的么?

这样看下来应该是 current_cart 函数的问题,看一下这个函数是不是和书上一样。。。然后注意一下冒号,大小写,单复数。。。。。。错误信息少了点,没法帮你精确定位

#7 楼 @davidqhr class CreateLineItems < ActiveRecord::Migration def change create_table :line_items do |t| t.integer :product_id t.integer :car_di

t.timestamps end end end

t.integer :car_di....... 你应该发现了点什么吧。。。

#8 楼 @richard020389 没错吧 我一字一字的比的...

#10 楼 @davidqhr 懂了 直接在 migrate 文件里改就行了?

修改,然后, rake db:drop rake db:migration

#13 楼 @davidqhr ok 谢谢!!!!!

lz 看的 3.0.5?

#15 楼 @small_fish__ 如果是在学敏捷开发的那本书的 不如在这儿http://intertwingly.net/projects/AWDwR4/checkdepot-31/section-6.3.html 我买了本中文的 不给力。感觉没这上面的全

You need to Sign in before reply, if you don't have an account, please Sign up first.