无法访问 class nil Rails 4 的对象

Can't access object of class nil Rails 4

我在 Rails 4

中进行测试时收到此错误消息
Error:
ArticlesControllerTest#test_x:
ActionView::Template::Error: undefined method `state' for nil:NilClass

错误发生在视图中的这一行:

<% @articles.each do |article| %>
    <div id="article-container">
      <div id="article-content">
        <ul>
          <li> <b>Status:</b> <%= article.status_id %></li>
          <li> <b>State:</b> <%= article.state.state %></li

>

文章的模型看起来像这样

  belongs_to :user
  belongs_to :book, primary_key: :ISBN, foreign_key: :ISBN
  has_one :state, primary_key: :states_id, foreign_key: "id"
  has_one :status, primary_key: :status_id, foreign_key: "id"

  has_and_belongs_to_many :courses

  validates :ISBN, presence: true
  validates :states_id, presence: true
  validates :user_id, presence: true
  validates :price, presence: true
  validates :status_id, presence: true

这是文章并在架构table中声明

文章table:

  create_table "articles", force: :cascade do |t|
    t.string   "ISBN"
    t.integer  "user_id"
    t.integer  "price"
    t.integer  "status_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "states_id"
    t.index ["states_id"], name: "index_articles_on_states_id"
  end

状态table:

  create_table "states", force: :cascade do |t|
    t.string   "state"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

问题是运行这个测试(从顶部显示文件直到有问题的测试):

require 'test_helper'

class ArticlesControllerTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers
  setup do
    @article_normal = articles(:normal_article)
    @article_admin = articles(:admin_article)
    @normal_user = users(:normal_user)
    @admin_user = users(:admin_user)

    @normal_user.id = @article_normal.user_id
    @admin_user.id = @article_admin.user_id
  end
  test "test x" do
    get articles_url
    assert_response :success
  end

测试提供了错误消息,而在本地主机中它有效。

编辑 测试数据库中填充了固定装置。

文章夹具

normal_article:
  ISBN: 9788252179675
  user_id: 1
  price: 500
  states_id: 1
  status_id: 1


admin_article:
  ISBN: 9788252179675
  user_id: 2
  price: 400
  states_id: 1
  status_id: 1

来自article_controller的控制器方法:

# GET /articles
  # GET /articles.json
  def index
    @articles = Article.all
    @courses = Course.all
    @states = State.all
    if params[:search]
      @articles = Article.search(params[:search])
    else
      @articles = Article.all
    end
  end

article 灯具没有正确引用 state 灯具。

不是通过固定的 id 引用 state(你在你的装置中使用:state_id: 1),而是给你的状态装置中的每个 state 一个合适的名称并像这样使用该名称:state: valid_state


引自 Rails Guide 关于如何在灯具中使用关联:

If you are working with associations, you can simply define a reference node between two different fixtures. Here's an example with a belongs_to/has_many association:

# In fixtures/categories.yml
about:
 name: About

# In fixtures/articles.yml
first:
  title: Welcome to Rails!
  body: Hello world!
  category: about

Notice the category key of the first article found in fixtures/articles.yml has a value of about. This tells Rails to load the category about found in fixtures/categories.yml.

For associations to reference one another by name, you can use the fixture name instead of specifying the id: attribute on the associated fixtures. Rails will auto assign a primary key to be consistent between runs. [...]