[英]Rails active storage - Download link
我正在嘗試創建一個帶有活動存儲的下載鏈接,以下載使用
<%= link_to 'download', rails_blob_path(f, disposition: "attachment") %>
但相反,它向我顯示undefined method filename for #<Order id: 1, paper_size: A4....
我怎樣才能解決這個問題??
index.html.erb
:
<div class="h1">Admin Dashboard</div>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Phone</th>
<th scope="col">Email</th>
<th scope="col">Size</th>
<th scope="col">Color</th>
<th scope="col">Type</th>
<th scope="col">Quantity</th>
<th scope="col">Description</th>
<th scope="col">Downloads</th>
</tr>
</thead>
<tbody>
<% @orders.each do |f| %>
<tr>
<th scope="row"><%= f.id %></th>
<td><%= f.first_name %></td>
<td><%= f.last_name %></td>
<td><%= f.phone_number %></td>
<td><%= f.email %></td>
<td><%= f.paper_size %></td>
<td><%= f.color %></td>
<td><%= f.paper_style %></td>
<td><%= f.quantity %></td>
<td><%= f.description %></td>
<% if f.files.attached? %>
<td><%= link_to 'download', rails_blob_path(f, disposition: "attachment") %></td>
<% end %>
<% end %>
</tr>
</tbody>
</table>
根據教程和文檔,它說我們需要使用rails_blob_path
function 來創建下載,但是當我使用它時,我收到一條錯誤消息,提示“未定義的方法文件名”。
當我使用時,我正在嘗試在表格內創建下載鏈接:
<% if f.files.attached? %>
<td><%= link_to 'download', root_url %></td>
<% end %>
它工作並將我重定向到根路徑,這表明f.files.attached?
正在返回 TRUE,但是當我調用rails_blob_path
function 時它不起作用。
您需要將 Active Storage 實例傳遞到rails_blob_path
路徑(而不是 Active Record 父對象)。 由於您似乎有多個文件,因此它應該類似於(遍歷所有“文件”並為每個文件創建一個鏈接):
<% f.files.each do |file| %>
<%= link_to 'download', rails_blob_path(file, disposition: "attachment") %>
<% end %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.