首页 > 解决方案 > Rails:未定义的方法 `signed_id' 用于#

问题描述

我在 Ruby 2.4.1-rc2 和 Rails 5.2.1 上,并且正在使用活动存储。文件上传是完美的,但是当我尝试使用rails_blob_path(user.avatar, disposition: "attachment", only_path: true)or调用文件下载 url 时Rails.application.routes.url_helpers.rails_blob_path(c.allegati, only_path: true),它给了我这个错误:

NoMethodError at /profilepage
undefined method `signed_id' for #<ActiveStorage::Attached::Many:0x00007f85c90dd170>

0x00007f85c90dd170 每次重新加载时都会更改。

我在这里和谷歌上搜索过,但没有人有这个问题,我的 ActiveStorage 配置是标准配置(S3 凭据除外)。

有任何想法吗?提前致谢。

编辑:更多代码

显示.html.erb

  <% if @utente.sostitutore == "1" %>
  <h1 class="title is-3">Sostituzioni accettate</h1>
  <% b = Sostituzione.where(sostitutore: utente_corrente.id) %>
  <% if b.empty? %>
  <p>
  Nessuna sostituzione accettata
  </p>
  <br />
  <% else %>
        <% b.each do |c| %>
            <%= Rails.application.routes.url_helpers.rails_blob_path(c.allegati, only_path: true) %>

  <% end %>
  <% end %>
  <% end %>

模型

class Sostituzione < ApplicationRecord
   attr_accessor :termini
   has_many_attached :allegati
   has_many_attached :documentiudienza
end

Edit2:尽管它已正确上传,但它通过调试空产生。

解决了

标签: ruby-on-railsrubyrails-activestorage

解决方案


解决了!对于多个附件(意大利语中的 allegati),有 @sostituzione.allegati.first 等。我用这段代码解决了:

<% @sostituzione.allegati.each do |allegato| %>
<div class="level">
  <%= link_to 'Scarica documento allegato', rails_blob_path(allegato, only_path: true), class: 'button is-primary is-big' %>
</div>
<% end %>

推荐阅读