【问题标题】:Getting error while displaying image "Can't resolve image into URL: undefined method `attachment_url' for #<ActionView::Base:0x0000000000b770>"显示图像时出错“无法将图像解析为 URL:#<ActionView::Base:0x0000000000b770> 的未定义方法 `attachment_url'”
【发布时间】:2021-08-21 13:37:50
【问题描述】:

这是我的模型的样子。

class Bug < ApplicationRecord
    has_many :bug_users, dependent: :destroy
    has_many :users , through: :bug_users  
    belongs_to :project
    has_one_attached :screenshot
end

上传部分工作得很好,但是当我在这样的视图中显示它时

<% @bugs.each do |bug| %>
<p>
   <strong>Title:</strong>
   <%= bug.title %>
</p>
<p>
<%if bug.screenshot.present? %>
   <strong>Screenshot:</strong>
   <%= image_tag bug.screenshot %>
<%end%>
</p>
<% end%>

I am getting the error

【问题讨论】:

  • 试试:&lt;%= image_tag url_for(bug.screenshot) %&gt;
  • 现在给出:#<:base:0x0000000000c0a8> 的未定义方法 `attachment_path'
  • 也许 bug.screenshot.attached? 返回 false。
  • 同时检查是否有帮助stackoverflow.com/questions/61715426/…
  • @LamPhan nah 我试过没有 if 语句它仍然给出同样的错误:(

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


【解决方案1】:

解决了!这有点疯狂,实际上我看到了一个他正在生成模型的教程:

rails g model User avatar:attachment

所以,我想也许我必须在我的模型中添加一列,当我这样做时:

# Could not dump table "bugs" because of following StandardError 
# Unknown type 'attachment' for column 'screenshot'

在我的 schema.rb 中,但它确实解决了错误,然后我再次运行迁移以删除该列,令人惊讶的是它仍然没有给出此错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2020-06-25
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    相关资源
    最近更新 更多