【问题标题】:Rails Active Storage not display @user.banner_image [duplicate]Rails Active Storage 不显示@user.banner_image [重复]
【发布时间】:2019-09-19 02:36:27
【问题描述】:

所以我最近一直在使用 rails active storage 并遇到了一个问题。

user.rb
class User < ApplicationRecord
  has_one_attached :banner_image
end

show.html.haml
.profile-header
  = @user.banner_image

作为回报,我得到了这个错误:

Can't resolve image into URL: to_model delegated to attachment, but attachment is nil
Extracted source (around line #2):

.profile-header
  = image_tag @user.banner_image

.listings
  - @listings.each do |listing|
    .box

我做错了什么? 顺便说一句,我在用户控制器中允许了banner_image。我已经安装了活动存储。

【问题讨论】:

    标签: ruby-on-rails ruby rails-activestorage


    【解决方案1】:

    应该是:

    .profile-header
      = image_tag url_for(@user.banner_image)
    

    【讨论】:

    • 将 to_model 委托给附件,但附件为零
    【解决方案2】:

    你必须通过设置Rails.application.config.active_storage.service告诉Active Storage使用哪个服务

    在本地存储文件

    config.active_storage.service = :local
    

    【讨论】:

    • 这一切都在 development.rb 中了吗?
    • 我已经设置好了。给我 to_model 委托给附件,但附件是 nil
    猜你喜欢
    • 2019-01-01
    • 1970-01-01
    • 2022-11-28
    • 2021-09-14
    • 2018-12-02
    • 1970-01-01
    • 2020-11-04
    • 2019-11-01
    • 1970-01-01
    相关资源
    最近更新 更多