gpt4 book ai didi

ruby-on-rails - 我的测试正在通过。但是,未定义的方法 `reviews' 为 nil :NilClass when calling the page

转载 作者:行者123 更新时间:2023-12-03 16:02:45 27 4
gpt4 key购买 nike

大家下午好

我正在尝试构建一个烟草审查应用程序,只是为了学习更多的东西。我一直试图建立在我在 Rails 教程书中学到的东西的基础上,但是我不断遇到我不理解的砖墙和陷阱。对于 nil:NilClass 和我遇到的其他问题,我通常可以克服未定义的方法“something”,但这个问题真的让我感到困惑,因为我的测试通过了。

我的测试

review_test.rb

require 'test_helper'

class ReviewTest < ActiveSupport::TestCase

# Define a user for the test
def setup
@user = users(:shaun)
@review = @user.reviews.build( rating: 5,
comment:"This is a super snus",
tobacco_id: 1)
end

# Test should be valid
test "should be valid" do
assert @review.valid?
end

# Test the user id is present
test "user id should be present" do
@review.user_id = nil
assert_not @review.valid?
end

# Test the tobacco id is present
test "tobacco id should be present" do
@review.tobacco_id = nil
assert_not @review.valid?
end

# Rating should be present
test "rating should be present" do
@review.rating = " "
assert_not @review.valid?
end

# Comment should be present
test "comment should be present" do
@review.comment = " "
assert_not @review.valid?
end

# Verify that the first review in the DB is the same as fixture review
test "order should be most recent first" do
assert_equal reviews(:most_recent), Review.first
end

end

user_test.rb
# Test that a review is destroyed when a user is destroyed
test "associated reviews should be destroyed" do
@user.save
@user.reviews.create!( rating: 5,
comment: "I love this suns, it's so good!",
tobacco_id: 1)
assert_difference 'Review.count', -1 do
@user.destroy
end
end

灯具

评论.yml
one:
rating: 5
tobacco_id: 1
comment: "This is a super snus"
created_at: <%= 3.hours.ago %>

two:
rating: 5
tobacco_id: 1
comment: "This is the best snus in the world"
created_at: <%= 2.hours.ago %>

three:
rating: 5
tobacco_id: 1
comment: "I will always buy this snus"
created_at: <%= 1.hours.ago %>

most_recent:
rating: 5
tobacco_id: 1
comment: "I have a shed load of this snus, it's great!"
created_at: <%= Time.zone.now %>

型号

review.rb
belongs_to :tobacco

烟草.rb
has_many :reviews

user.rb
has_many :tobaccos, dependent: :destroy
has_many :reviews, dependent: :destroy

Controller
review_controller.rb
def new
@review = Review.new
end

这是我在 Controller 中创建操作的内容,但我不知道我是否正确,因为我还无法加载页面。如果我有这个错误,我将不胜感激任何关于创建操作的建议,以避免问另一个问题,我可能有!
def create
@tobacco = Tobacco.find(params[:tobacco_id])
@review = @tobacco.reviews.new(params[:review].permit(:tobacco_id, :comment))
@review.user = User.find(current_user.id)
@review.save
redirect_to tobacco_path(@tobacco)
end

路线
resources :tobaccos do 
resources :reviews, except: [:show, :index]
end

show.html.erb
<%= link_to "Write a Review", new_tobacco_review_path(@tobacco) %>

new.html.erb
<%= form_for([@tobacco, @tobacco.reviews.build]) do |f| %>
<%= render 'fields', f: f %>
<%= f.submit "Save Review", class: 'btn btn-primary' %>
<% end %>

当我进入页面写评论时,我收到错误
Showing C:/Sites/apps/review/app/views/reviews/new.html.erb where line #11 raised:

undefined method `reviews' for nil:NilClass

它告诉我这是第 11 行
<%= form_for([@tobacco, @tobacco.reviews.build]) do |f| %>

但无论我尝试什么,我都会遇到不同的错误。我知道这是菜鸟的错误,但我看不到。

提前致谢

肖恩

更新

我已经让它工作并现在保存到数据库中,我是这样做的
在 review_controller.rb 中,新的和创建的看起来像这样
def new
@tobacco = Tobacco.find(params[:tobacco_id])
end


def create

@tobacco = Tobacco.find(params[:tobacco_id])
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.tobacco_id = @tobacco.id

if @review.save
redirect_to @tobacco
else
render 'new'
end
end

review_params 是
def review_params
params.require(:review).permit(:rating, :comment, :tobacco_id)
end

对我来说,这一切看起来都不错,而且很有效,所以我希望一切顺利。

我现在被困在编辑属于烟草的评论。这是我第一次处理嵌套路由。我只是看了几个教程,但没有什么能真正帮助我解决这个问题。我不确定如何传递这些 key ...

在节目页面上我有这个
<%= link_to "Edit", edit_tobacco_review_path(@tobacco, @review) %>

显示方法烟草_ Controller .rb 看起来像这样
def show
@tobacco = Tobacco.find(params[:id])
@reviews = Review.where(tobacco_id: @tobacco.id)
end

在 review_controller.rb 我有这个
def edit
@tobacco = Tobacco.find(params[:tobacco_id])
@review = Review.find(params[:id])
end

我得到的错误是这个
ActionController::UrlGenerationError in Tobaccos#show
No route matches {:action=>"edit", :controller=>"reviews", :id=>nil, :tobacco_id=>"8"} missing required keys: [:id]

看着这个,我应该在 Tobaccos#show 行动中做点什么,但我想不出是什么。

我快回家了,你能看出我做错了什么吗?谢谢 :)

更新编号 2 show.html.erb
这不是整个节目页面,但它是重要的部分
<% if @reviews.blank? %>
<h3>No reviews yet! Would you like to be the first?</h3>
<%= link_to "Write Review", new_tobacco_review_path(@tobacco), class: "btn btn-danger" %>
<% else %>
<% @reviews.each do |review| %>
<div class="reviews">
<p><%= review.comment %></p>
<p><%= review.user.name %></p>
<%= link_to "Edit", edit_tobacco_review_path(@tobacco, @review) %>
</div>
<% end %>
<%= link_to "Write Another Review", new_tobacco_review_path(@tobacco), class: "btn btn-danger" %>
<% end %>

更新编号 3 edit.html.erb
<%= form_for([@tobacco, @tobacco.reviews.build]) do |f| %>
<%= render 'fields', f: f %>
<%= f.submit "Update Review", class: 'btn btn-primary' %>
<% end %>
<%= link_to "Back", tobacco_path(@tobacco),
class: "btn btn-default btn-block margin-top-25" %>

_fields.html.erb
<%= f.label  :rating %>
<%= f.select :rating, [['1', 1],
['2', 2],
['3', 3],
['4', 4],
['5', 5]
] ,class: 'form-control' %>

<%= f.label :comment %>
<%= f.text_field :comment, class: 'form-control' %>

最佳答案

ActionController::UrlGenerationError in Tobaccos#show No route matches {:action=>"edit", :controller=>"reviews", :id=>nil, :tobacco_id=>"8"} missing required keys: [:id



你在迭代 @reviewsreview , 所以

这条线
<%= link_to "Edit", edit_tobacco_review_path(@tobacco, @review) %>

应该
<%= link_to "Edit", edit_tobacco_review_path(@tobacco, review) %>

关于ruby-on-rails - 我的测试正在通过。但是,未定义的方法 `reviews' 为 nil :NilClass when calling the page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31677043/

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