私はAgile Web Developmentのブックチュートリアルを少し変更して、チェックアウトの第12章の途中で行っています。Rails 3.2 NoMethodError(アジャイルブックを使用したline_itemの未定義メソッド `price = ')
私は、次のエラーを取得しています:ここで
NoMethodError (undefined method `price=' for #<LineItem:0x00000103a0de18>):
app/models/cart.rb:11:in `add_deal'
app/controllers/line_items_controller.rb:45:in `create'
は私のカート・モデルである:
:ここclass Cart < ActiveRecord::Base
# attr_accessible :title, :body
has_many :line_items, dependent: :destroy
def add_deal(deal_id)
current_item = line_items.find_by_deal_id(deal_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(deal_id: deal_id)
current_item.price = current_item.deal.price
end
current_item
end
def total_price
line_items.to_a.sum { |item| item.total_price }
end
end
は私のそれがフリーズ該当する行45でline_items_controllerでアクションを作成することです
def create
@cart = current_cart
deal = Deal.find(params[:deal_id])
@line_item = @cart.add_deal(deal.id)
マイラインアイテムモデル:
私はコンソールを使用してみましたclass Deal < ActiveRecord::Base
attr_accessible :description, :expiration, :featured, :image_url, :inventory, :price, :sold, :title, :value, :deal_id
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :title, :description, :image_url, presence: true
private
# ensure that there are no line items referencing this product
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
end
、item.deal.priceはitem.priceだけで正常に動作しなく:210
class LineItem < ActiveRecord::Base
attr_accessible :cart_id, :deal_id, :quantity
belongs_to :order
belongs_to :deal
belongs_to :cart
def total_price
deal.price * quantity
end
end
は、ここに私の取引モデルです。
line_itemモデルでは、attr_accessible:priceを試しましたが、何も修正しませんでした。
私は自分のコードと本をチェックしたが、大きな違いは何も分からなかった。
1つのアイデアは、LineItemsの価格のためのデータベースフィールドを設定することですが、本はそれを実行せず、DRY原則に違反します。
私は何時間もソースコードを見つめて何か間違ったものを見つけることができないので、どんな助力もあれば幸いです。ありがとう。
ありがとうございます!私が以前に頼んだことを願っていましたが、DRYをよく理解するのは良いことです。 – Castielle
あなたは本の図5.3を見ましたか?それが合法かどうかわからないので、私はここに投稿することをためらった。 – jdoe
ああ、これらの図面は理解に役立ちます。私はそこにマイグレーションのリストがないので、本に誤字があると思います。 – Castielle