如何为RSPEC创建用于Activestorage变体的固定装置?
关于个人项目的摘要
,我有一种根据视口大小来引用Activestorage Resource的变体的观点。我正在尝试确保视图也使用规格中的正确变体,但是正在遇到nomethoderror
问题。
分解
例如,
<div>
<%= image_tag @post.image.variant(:web).processed.url %>
</div>
为以下视图部分:和此规格:
feature 'Posts', type: :feature do
fixtures :posts, :users
scenario 'Accessing a public post via ID' do
visit post_path(posts(:one).id)
expect(page).to have_css("#post-#{posts(:one).id}")
end
end
以及这些固定装置(定义为官方指南):
posts.yml
:
one:
user: rafael
filter: gotham
private: false
caption: This is a beautiful fixture
active_storage/actionments.yml
:
one:
name: image
record: rafael (User)
blob: one_blob
active_storage/blobs.ymls.yml
:
one_blob:
<%= ActiveStorage::FixtureSet.blob filename: 'storage/brighton-pier.jpg', service_name: 'test' %>
视图无法呈现:
NoMethodError:
undefined method `processed' for nil:NilClass
这是有道理的,因为固定装置没有定义的变体。
编辑#1
POST
模型具有一组预定义的变体:
class Post < ApplicationRecord
belongs_to :user
has_many :comments, dependent: :destroy
has_one_attached :image, dependent: :destroy do |attachable|
attachable.variant :web, resize_to_fill: [1200, 1200, { crop: :attention }]
attachable.variant :opengraph, resize_to_fill: [200, 630, { crop: :attention }]
attachable.variant :twitter, resize_to_fill: [1024, 512, { crop: :attention }]
attachable.variant :mobile, resize_to_fill: [600, 600, { crop: :attention }]
attachable.variant :thumb, resize_to_fill: [100, 100, { crop: :attention }]
end
...
end
问题
如何在Activestorage固定装置中添加变体?
Summary
On a personal project, I have a view that references an ActiveStorage resource's variant, based on viewport size. I'm trying to ensure that the views use the correct variants in the specs as well, but am running into NoMethodError
problems.
Breakdown
For example, with the following view partial:
<div>
<%= image_tag @post.image.variant(:web).processed.url %>
</div>
And this spec:
feature 'Posts', type: :feature do
fixtures :posts, :users
scenario 'Accessing a public post via ID' do
visit post_path(posts(:one).id)
expect(page).to have_css("#post-#{posts(:one).id}")
end
end
And these fixtures (defined as per the official guide):
posts.yml
:
one:
user: rafael
filter: gotham
private: false
caption: This is a beautiful fixture
active_storage/attachments.yml
:
one:
name: image
record: rafael (User)
blob: one_blob
active_storage/blobs.yml
:
one_blob:
<%= ActiveStorage::FixtureSet.blob filename: 'storage/brighton-pier.jpg', service_name: 'test' %>
The views fail to render with:
NoMethodError:
undefined method `processed' for nil:NilClass
This makes sense, because the fixtures do not have variants defined.
Edit #1
The Post
model has a set of predefined variants:
class Post < ApplicationRecord
belongs_to :user
has_many :comments, dependent: :destroy
has_one_attached :image, dependent: :destroy do |attachable|
attachable.variant :web, resize_to_fill: [1200, 1200, { crop: :attention }]
attachable.variant :opengraph, resize_to_fill: [200, 630, { crop: :attention }]
attachable.variant :twitter, resize_to_fill: [1024, 512, { crop: :attention }]
attachable.variant :mobile, resize_to_fill: [600, 600, { crop: :attention }]
attachable.variant :thumb, resize_to_fill: [100, 100, { crop: :attention }]
end
...
end
Question
How do I add variants to my ActiveStorage fixtures?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论