【发布时间】:2017-05-04 02:32:31
【问题描述】:
我正在使用 Rails 5 和 Paperclip 创建一个图像上传应用程序。我可以在索引页面的列表视图中完美地显示上传的图像。我为每个图像设置都有一个“显示”链接,以及控制器中的显示路由和显示方法。但是,我无法弄清楚如何通过 show.html.erb 页面上的 :id 正确显示图像。从 调用图像不适用于在 show.html.erb 上显示。在用户单击索引页面中的“显示”链接后,我只想在 images/show/:id 页面上显示单个图像。
这是我的模型(这可能是问题...我需要 display_url 吗?):
class Image < ApplicationRecord
has_attached_file :avatar
validates_attachment_content_type :avatar,
:content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
这是Controller中的Show方法
def show
@image = Image.find(params[:id])
end
show.html.erb(此代码是这里的问题)
<% image_tag @image.avatar.url %>
这是我在 index.html.erb 中的图像列表视图(工作 100%):
<% if @images %>
<% @images.each do |image| %>
<%= image_tag image.avatar.url %>
<%= image_tag image.title, {class: "list-group-item"} %>
<a href="/images/<%= image.id %>">Display</a>
<a href="/images/edit/<%= image.id %>">Edit</a>
<% end %>
<% end %>
【问题讨论】:
标签: ruby rubygems paperclip ruby-on-rails-5