Rails ActiveStorage url_for 返回一个无效的 URL [英] Rails ActiveStorage url_for returns an URL which is not valid

查看:30
本文介绍了Rails ActiveStorage url_for 返回一个无效的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有头像的员工模型.我可以将图像附加到头像,但是每当我尝试显示图像时,

I have an Employee model with an Avatar. I can attach an image to the avatar, but whenever I try to display the image, the

url_for(@employee.avatar)

产生死链接.我所看到的只是来自标签的 alt 属性的值.我得到的图像标签如下

produces a dead link. All I'm seeing is the value from the alt attribute from the tag. The image tag I'm getting is the following

<img src="/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--4786aa4d9d82d8f8d572b857965088b20fcb2f49/Portrait.jpg" 
alt="Thumbnail">

而且我知道图片已正确附加.当我调用以下代码时,我得到了这个结果:

And I know the image has been properly attached. When I call the following, I get this result:

@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> 

因为我可以在存储目录中看到图像

as I can see the image in the storage directory

非常感谢帮助.谁能帮我显示保存的图像.

Help is highly appreciated. Can anyone help me display the saved image.

这是我的设置.

class Employee < ApplicationRecord
   has_one_attached :avatar
   ...
end

我的 storage.yml 文件的内容

Content of my storage.yml file

local:
   service: Disk
   root: <%= Rails.root.join("storage") %>

我从 Active Storage 的迁移已迁移.查看我的 schema.rb 文件

My the migrations from Active Storage are migrated. See my schema.rb file

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 将路由附加到主路由文件中,因此它们会在您捕获所有路由之后出现.更好的是你可以逃避像

Since Active Storage appends the routes in the master route file, so they come after you catch all the routes. Better you can escape the active storage routes like

get '*path', to: redirect('/'), constraints: lambda { |req|
  req.path.exclude? 'rails/active_storage'
}

这篇关于Rails ActiveStorage url_for 返回一个无效的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆