我一直在尝试使用带有 Rails 的 RSpec 测试 Paperclip 文件上传。
当我运行服务器时,我没有任何问题,但是当我运行我的(非常基本的)RSpec 测试时,我收到以下错误:
Failure/Error: visit root_path
ActionController::UnknownFormat:
PhotosController#index is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
这是我的测试:
require 'rails_helper'
feature 'photos' do
context 'no photos have been added' do
scenario 'should display prompt to add a photo' do
visit root_path
expect(page).to have_content 'No photos yet'
expect(page).to have_link 'Add a photo'
end
end
end
这是我对haml的看法:
- if @photos.any?
- @photos.each do |photo|
= image_tag photo.image
= photo.caption
- else
%h1 No photos yet
%h2= link_to "Add a photo", new_photo_path
这是我的照片模型:
class Photo < ApplicationRecord
validates :image, presence: true
has_attached_file :image, styles: { :medium => "640x" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
我在测试中遇到了 NoMethodError 的一些问题,说它has_attached_file
无法识别,但是在广泛使用谷歌搜索之后,我发现有人通过将 paperclip.rb 文件添加到 config/initializers 并使用以下内容很幸运:
require "paperclip/railtie"
Paperclip::Railtie.insert
知道为什么我可能会遇到这个问题,以及如何克服它吗?