初めに
作成していたアプリケーションのrspecが突然動かなくなったので自分が試したことを書きます。
突然出たActionView::Template::Error (Can't resolve image into URL: undefined method `[]' for nil:NilClass)
以前まではrspecを実行したとしても```ActionView::Template::Error (Can't resolve image into URL: undefined method [] for nil:NilClass)`といった内容のエラーが出ていなかったのですが、突然出るようになり困惑しました。
$ bundle exec rspec
[2018-12-08T16:01:40.215545 #73536] INFO -- : Started GET "/~/categories/1" for 127.0.0.1 at 2018-12-08 16:01:40 +0900
F, [2018-12-08T16:01:43.018422 #73536] FATAL -- :
F, [2018-12-08T16:01:43.018476 #73536] FATAL -- : ActionView::Template::Error (Can't resolve image into URL: undefined method `[]' for nil:NilClass):
F, [2018-12-08T16:01:43.018577 #73536] FATAL -- : 1: <div class="col-sm-4 col-xs-12">
2: <div class="productBox">
3: <div class="productImage clearfix">
4: <%= image_tag(product.display_image.attachment.url(:product)) %>
5: <%= render "potepan/shared/product_masking" %>
6: </div>
7: <div class="productCaption clearfix">
試したこと
binding.pryをrspecのテスト文に挿入して
$ bundle exec rspec spec/features/categories_spec.rb:14
From: /Users/~/environment/~/spec/features/categories_spec.rb @ line 14 :
9:
10: background { visit potepan_category_path(taxon.id) }
11:
12: scenario "Display products of selected categories on the screen" do
13: binding.pry
=> 14: expect(page).to have_link product.name, href: potepan_product_path(product.id)
15: expect(page).to have_content product.display_price
16: expect(page).to have_link product_child.name, href: potepan_product_path(product_child.id)
17: expect(page).to have_content product_child.display_price
18: expect(page).to have_link(
19: taxon_child.name, href: potepan_category_path(taxon_child.id)
[1] pry(#<RSpec::ExampleGroups::CategoriesShow>)> Spree::Product.first.image
NoMethodError: undefined method `image' for #<Spree::Product:0x00007fa69093fa88>
Did you mean? images
from /Users/~/environment/~/vendor/bundle/ruby/2.5.0/gems/activemodel-5.2.1/lib/active_model/attribute_methods.rb:430:in `method_missing'
[2] pry(#<RSpec::ExampleGroups::CategoriesShow>)> Spree::Product.first.images
=> []
[3] pry(#<RSpec::ExampleGroups::CategoriesShow>)> Spree::Product.first.display_image
=> #<Spree::Image:0x00007fa690c24aa0
id: nil,
viewable_type: nil,
viewable_id: nil,
attachment_width: nil,
attachment_height: nil,
attachment_file_size: nil,
position: nil,
attachment_content_type: nil,
attachment_file_name: nil,
type: "Spree::Image",
attachment_updated_at: nil,
alt: nil,
created_at: nil,
updated_at: nil>
pryの結果を見ても、product.display_image.attachment.url(:product)は問題ないのかもしれません...ということは、image_tagに問題があるのかもしれないので、エラーが出ているファイルに何が起きているのかを把握するために、ファイルの頭に以下の文を挿入して
<%
puts '----------'
begin
puts product.display_image.attachment
puts product.display_image.attachment.url(:product)
puts image_tag(product.display_image.attachment.url(:product))
rescue => e
puts e.message
puts e.backtrace
end
puts '----------'
%>
<div class="col-sm-4 col-xs-12">
<div class="productBox">
<div class="productImage clearfix">
<%= image_tag(product.display_image.attachment.url(:product)) %>
bundle exec rspec -e 'Display' spec/features/categories_spec.rbを実行
するとvendor/bundle/ruby/2.5.0/gems/actionview-5.2.1/lib/action_view/helpers/asset_tag_helper.rb:473の以下のところに問題があると出てきました。
def resolve_image_source(source, skip_pipeline)
if source.is_a?(Symbol) || source.is_a?(String)
path_to_image(source, skip_pipeline: skip_pipeline)
else
polymorphic_url(source) #ここ?
end
rescue NoMethodError => e
raise ArgumentError, "Can't resolve image into URL: #{e}"
end
image_tag -> resolve_image_sourceからの、 polymorphic_url(source) へ進んだ先で to_model に反応できなくなって落ちるようになった...?。
結局
最終的に何を行ったかといいますと
bundle updateです。
何故、bundle updateでなぜ解消するのか、よくわかりません。通常、バージョンの異なるgemがインストールされていてもGemfile.lockに書かれてあるバージョンしか使用しないので問題ないはずだと認識していました...
根本的な解決になっていない気がするのですが、同じようなエラーで困っている方がいれば何か少しでも参考になれば幸いです。