gpt4 book ai didi

ruby-on-rails - 无法访问类 nil Rails 4 的对象

转载 作者:行者123 更新时间:2023-11-28 20:38:42 25 4
gpt4 key购买 nike

我在 Rails 4 中做测试时得到这个错误消息

Error:
ArticlesControllerTest#test_x:
ActionView::Template::Error: undefined method `state' for nil:NilClass

错误发生在 View 中的这一行:

<% @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

这是模式中的文章和状态表

文章表:

  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

状态表:

  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 的 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 fixture 没有正确引用 state fixture。

不是通过固定的 id 来引用 state(您在 fixture 中使用:state_id: 1),而是给每个 state 在你的 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. [...]

关于ruby-on-rails - 无法访问类 nil Rails 4 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42861066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com