【问题标题】:Can't mass-assign protected attributes: item无法批量分配受保护的属性:项目
【发布时间】:2014-08-14 21:48:32
【问题描述】:

我正在为 rails 3 使用那个 gem https://github.com/crowdint/acts_as_shopping_cart 的版本

我用约定名称做了所有事情。但我收到错误无法批量分配受保护的属性:项目

class ShoppingCartsController < ApplicationController
  before_filter :extract_shopping_cart
  def create
    @product = Video.find(params[:product_id])
    @shopping_cart.add(@product, @product.price) # the error is generated on that line
    redirect_to shopping_cart_path
  end

我的模型购物车:

# == Schema Information
#
# Table name: shopping_carts
#
#  id         :integer          not null, primary key
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class ShoppingCart < ActiveRecord::Base
    acts_as_shopping_cart

    attr_accessible :price

    #accepts_nested_attributes_for :price
end

我的模型 ShoppingCartItem:

# == Schema Information
#
# Table name: shopping_cart_items
#
#  id         :integer          not null, primary key
#  owner_id   :integer
#  owner_type :string(255)
#  quantity   :integer
#  item_id    :integer
#  item_type  :string(255)
#  price      :float
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class ShoppingCartItem < ActiveRecord::Base
  attr_accessible :owner_id, :owner_type, :quantity, :item_id, :item_type, :price
  acts_as_shopping_cart_item
end

谁能帮助我这里缺少什么?

【问题讨论】:

  • 您是否尝试将 :item 添加到 attr_accessible ?
  • 是的,并且接受_nested_attributes_for
  • 把它放在两个模型中,看看它属于哪里

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


【解决方案1】:

希望你知道

Rails 3

从 0.2.0 版开始,Rails 3 不再受支持。如果您仍需要在 Rails 3 应用程序中实现此 gem,请使用 0-1-x 分支

gem 'acts_as_shopping_cart', :github => 'crowdint/acts_as_shopping_cart', :branch => '0-1-x'

【讨论】:

  • 是的,我看到了,我正在使用这个版本。谢谢!
猜你喜欢
  • 2011-09-04
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多