ruby-on-rails - Rails ActiveStorage url_for 返回一个无效的 URL
问题描述
我有一个带有头像的员工模型。我可以将图像附加到头像,但每当我尝试显示图像时,
url_for(@employee.avatar)
产生死链接。我所看到的只是标签中alt属性的值。我得到的图像标签如下
<img src="/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--4786aa4d9d82d8f8d572b857965088b20fcb2f49/Portrait.jpg"
alt="Thumbnail">
而且我知道图像已正确附加。当我调用以下命令时,我得到了这个结果:
@employee.avatar
=> #<ActiveStorage::Attached::One:0x00007ff7e9ba41c0 @name="avatar",
@record=#<Employee id: 4, first_name: "Meda", last_name: "Burgdorf",
created_at: "2019-03-03 23:03:00", updated_at: "2019-03-03 23:17:56">,
@dependent=:purge_later>
高度赞赏帮助。谁能帮我显示保存的图像。
这是我的设置。
class Employee < ApplicationRecord
has_one_attached :avatar
...
end
我的 storage.yml 文件的内容
local:
service: Disk
root: <%= Rails.root.join("storage") %>
我从 Active Storage 的迁移被迁移。查看我的 schema.rb 文件
ActiveRecord::Schema.define(version: 2019_03_03_211537) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.integer "record_id", null: false
t.integer "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end
create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end
解决方案
由于 Active Storage 将路由附加到主路由文件中,因此它们会在您捕获所有路由之后出现。更好的是,您可以逃脱活动的存储路线,例如
get '*path', to: redirect('/'), constraints: lambda { |req|
req.path.exclude? 'rails/active_storage'
}
推荐阅读
- sql - 如何将 Excel 中的数据插入 Spark SQL 中的临时表中
- python - 合并 2 个字典并将它们存储在 pandas 数据框中,其中一个字典具有可变长度列表元素
- python - 无法在 Python 中将 Matlab 时间戳转换为日期时间
- sql-server - 如何在名称中创建具有动态约束 UUID 的临时表
- android - 是否有适用于 Wear OS 应用程序的时间选择器小部件?
- html - 如何使用 CSS 将单词换到下一行?
- c# - 在特定单词之后以字符串格式提取部分 URL
- javascript - HTML / CSS - 如何使覆盖视频文本完全响应?
- swift - 从硬编码转换为从 plist 读取
- php - 我需要在 MacOSX 上使用 php 连接一个 php MyAdmin MySQL 数据库,但它无法连接