0

別の初心者の質問を前もってお詫びしますが、ROR構文は私と一緒にクリックしていないだけです.ショートカットと慣習に頭を悩ませることはできません(すでに数冊の本を読んでいるにもかかわらず!) -

これを本から効果的にコピーしましたが、ビルド、作成などとは何かを理解しようとしていますか?

@cart = current_cart
product = Catalog::Product.find(params[:product_id])
Rails.logger.debug { "Value in cart id " + @cart.id.to_s }

@checkout_line_item = @cart.line_items.build(product: product)

respond_to do |format|
  if @checkout_line_item.save...

ログからの出力は次のとおりです。

Processing by Checkout::LineItemsController#create as HTML
Parameters: {"utf8"=>"✓",  "authenticity_token"=>"9NH+xgDPTf/iN7RCdPd8H9rAIqWsSVB/f/rIT++Kk7M=", "product_id"=>"7"}
Created a line item
(0.1ms)  BEGIN
SQL (2.0ms)  INSERT INTO `checkout_carts` (`created_at`, `discounts`, `grand_total`,   `loyalty_points`, `order_date`, `subtotal`, `timestamps`, `total_tax`, `updated_at`) VALUES    ('2012-08-21 11:06:15', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2012-08-21 11:06:15')
(0.2ms)  COMMIT
Catalog::Product Load (0.2ms)  SELECT `products`.* FROM `products` WHERE `products`.`id` =   7 LIMIT 1
Value in cart id 8
Completed 500 Internal Server Error in 5ms

NoMethodError (undefined method `save' for nil:NilClass):
app/controllers/checkout/line_items_controller.rb:55:in `block in create'
app/controllers/checkout/line_items_controller.rb:54:in `create'

問題は、チェックアウト項目を作成するビルド構文にあると推測しています。または、has_many 関連付けを間違って設定した可能性があります。誰かがトラブルシューティングを手伝ってくれるだけで十分ですか? または、モデル宣言を投稿する必要がありますか?

モデルで更新:

class Checkout::LineItem < ActiveRecord::Base
  attr_accessible :customer_update_date, :inventory_status, :line_item_color, :line_item_description, :line_item_size, :line_item_tagline, :line_item_total, :quantity, :sku_id, :style_id, :tax, :tax_code, :timestamps, :unit_price, :product
  belongs_to :cart
  belongs_to :product, :class_name => 'Catalog::Product'
end

class Checkout::Cart < ActiveRecord::Base
  attr_accessible :discounts, :grand_total, :loyalty_points, :order_date, :subtotal, :timestamps, :total_tax

  has_many :line_items, dependent: :destroy
end

module Catalog
  class Product < ActiveRecord::Base
    attr_accessible :assoc_product,:product_id, :merch_associations, :aux_description, :buyable, :long_description, :name, :on_special, :part_number, :release_date, :short_description, :withdraw_date, :occasion
   <<clipped for brevity>>

has_many :line_items, :class_name => 'Checkout::LineItem'
...
  end

自分の質問に答えることはできませんが、答えを得たと思います:

ビルド呼び出しにカートを追加する必要があったようです...

これはうまくいったようです(別のブロッキングの問題があると思いますが、それを分類できます):

@cart = current_cart
product = Catalog::Product.find(params[:product_id])
Rails.logger.debug { "Value in cart id " + @cart.id.to_s }

@checkout_line_item = @cart.line_items.build(product: product, cart: @cart)
4

1 に答える 1

0

Build基本的に、空のオブジェクト用のスペースを作成して予約します。これにより、関連付けられたオブジェクトを保存せずに作成できます。私がこれを経験した唯一の使用例は、イベントに複数の日付がある入れ子になったフォームであるため、5.times.build を使用して 5 つの空の日付関連付けを作成しました。

于 2017-01-03T18:27:17.853 に答える