导致此错误的原因是我的索引页中的代码是<%= pin.user.name %>
。
但是,当我将其更改为时<%= pin.user.name if pin.user %>
,我的测试全部通过了。
相同的问题已经发布,但是没有可接受的解决方案,这就是我发布此问题的原因。
这是完整的错误消息:
ERROR["test_non-signed_in_user_layout_links", SiteLayoutTest, 1.3199719070007632]
test_non-signed_in_user_layout_links#SiteLayoutTest (1.32s)
ActionView::Template::Error: ActionView::Template::Error: undefined method `name' for nil:NilClass
app/views/pins/_pin.html.erb:2:in `_app_views_pins__pin_html_erb___3167554587389080103_47088431630420'
app/views/static_pages/home.html.erb:6:in `_app_views_static_pages_home_html_erb___4092361779176196837_47088431162360'
test/integration/site_layout_test.rb:10:in `block in <class:SiteLayoutTest>'
ERROR["test_signed_in_user_layout_links", SiteLayoutTest, 2.0834625150000647]
test_signed_in_user_layout_links#SiteLayoutTest (2.08s)
ActionView::Template::Error: ActionView::Template::Error: undefined method `name' for nil:NilClass
app/views/pins/_pin.html.erb:2:in `_app_views_pins__pin_html_erb___3167554587389080103_47088457226940'
app/views/static_pages/home.html.erb:6:in `_app_views_static_pages_home_html_erb___4092361779176196837_47088457198480'
test/integration/site_layout_test.rb:24:in `block in <class:SiteLayoutTest>'
ERROR["test_should_get_home", StaticPagesControllerTest, 2.1026109900003576]
test_should_get_home#StaticPagesControllerTest (2.10s)
ActionView::Template::Error: ActionView::Template::Error: undefined method `name' for nil:NilClass
app/views/pins/_pin.html.erb:2:in `_app_views_pins__pin_html_erb___3167554587389080103_47088457310920'
app/views/static_pages/home.html.erb:6:in `_app_views_static_pages_home_html_erb___4092361779176196837_47088457291840'
test/controllers/static_pages_controller_test.rb:9:in `block in <class:StaticPagesControllerTest>'
这些是具有上述错误的测试:
site_layout_test.rb
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
def setup
ActionMailer::Base.deliveries.clear
@user = users(:john)
end
test "non-signed in user layout links" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", about_path
assert_select "a[href=?]", new_user_session_path, count: 2
assert_select "a[href=?]", new_user_registration_path, count: 1
end
test "signed in user layout links" do
get new_user_session_path
assert_template 'devise/sessions/new'
post user_session_path, params: { user: { email: @user.email, password: 'password' } }
assert_not flash.empty?
assert_redirected_to root_path
follow_redirect!
assert_template 'static_pages/home'
assert_select "a[href=?]", new_user_session_path, count: 0
assert_select "a[href=?]", new_user_registration_path, count: 0
assert_select "a[href=?]", edit_user_registration_path
assert_select "a[href=?]", destroy_user_session_path
assert_select "a[href=?]", new_pin_path
end
end
static_pages_controller_test.rb
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
def setup
@user = users(:john)
end
test "should get home" do
get root_url
assert_response :success
assert_select "title", full_title
end
test "should get about" do
get about_url
assert_response :success
assert_select "title", full_title("About")
end
end
这些是我的users.yml
:
john:
name: John Rockefeller
email: [email protected]
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
created_at: <%= Time.zone.now %>
andrew:
name: Andrew Carnegie
email: [email protected]
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
created_at: <%= Time.zone.now %>
和 pins.yml
quote:
description: "Lorem quote"
created_at: <%= 10.minutes.ago %>
user: john
recipe:
description: "Lorem recipe"
created_at: <%= 3.years.ago %>
most_recent:
description: "Lorem most recent"
created_at: <%= Time.zone.now %>
如果您binding.pry
对该代码段投掷(假设您有撬动/再见),并pin.user
在这种情况下键入,则应该说nil
。
你会得到当您尝试打电话给你目前得到的错误.name
上nil
。您的if语句为您节省的原因.name
是ifpin.user
为nil不会被调用。
在Rails中非常方便的解决方法是将您的代码更改为:
<%= pin.user.try(:name) %>
这不会引发错误,但只返回nil
如果pin.user
IS nil
。
如果您始终希望将用户连接到图钉,可以通过在创建图钉之前验证用户是否与该图钉相关联来防止无用户图钉的存在:
class Pin < ActiveRecord::Base
validates :user, presence: true
end
希望这可以帮助!!:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句