1

これは私の _line_items.text.erb ファイルにあります:

<%= sprintf("%2d x %s", line_item.quantity,
              truncate(line_item.product.title, length: 50)) %>

注文.yml

one:
  name: Dave Thomas
  address: MyText
  email: dave@example.org 
  pay_type: Check

line_items.yml

one:
  product: ruby
  cart_id: 1
  order: one

two:
  product_id: 1
  cart_id: 1
  order: one

製品.yml

ruby:
  title: Programming Ruby 1.9 
  description: 
    Ruby is the fastest growing and most exciting dynamic language out there. 
    If you need to get working programsdelivered fast, you should add Ruby to your toolbox.
  price: 49.50 
  image_url: ruby.png

これはすべて正しいようです。

実際のテストは次のとおりです。

class OrderNotifierTest < ActionMailer::TestCase
  test "received" do
    mail = OrderNotifier.received(orders(:one))
    assert_equal "Pragmatic Store Order Confirmation", mail.subject
    assert_equal ["dave@example.org"], mail.to
    assert_equal ["depot@example.com"], mail.from
    assert_match  /1 x Programming Ruby 1.9/, mail.body.encoded
  end

ActionView::Template::Error: undefined method 'title' for nil:NilClassエラーを探す他の場所についてのアイデアはありますか?

アップデート:

class LineItem < ActiveRecord::Base
  attr_accessible :cart_id, :product_id, :quantity, :order_id, :product, :cart, :price
  belongs_to :order
  belongs_to :cart
  belongs_to :product

  def total_price 
    self.price * self.quantity
  end
end
4

2 に答える 2

2

titleエラーの内容は、 type のオブジェクトで未定義のメソッドを呼び出そうとしていることですNilClass。提供したコード行を見ると、エラーがどこにあるのかを確認できるはずです。

<%= sprintf("%2d x %s", line_item.quantity,
          truncate(line_item.product.title, length: 50)) %>

読む部分がline_item.product.title問題です。product項目は nil でなければなりません。に変更することをお勧めします。これにより、 Rails の try helperline_item.product.try(:title)が利用され、nil のイベントで nil エラーがスローされるのを防ぐことができます。product

フィクスチャが正しく記述されていないようです。 line_item# 2 に問題があります... line_item #2 を product_id: 1 の代わりに product: ruby​​ に変更する必要があります。これで修正されるはずです。フィクスチャのドキュメントに記載されているとおり (以下を参照)。

次のように、テストでアクセスできるようにフィクスチャを定義する必要があるようにも見えます。

class OrderNotifierTest < ActionMailer::TestCase
  fixtures :orders, :line_items, :products
  ...

詳細については、 Rails Fixture のドキュメントを参照してください。(具体的には、「フィクスチャの使用」というタイトルのセクションです。)

于 2012-07-20T20:41:17.850 に答える