我想使用Rails应用程序上传图像。最初,我使用回形针,但现在还没有开发回形针,所以我决定切换到内置的rails机会。
嗯,我做了文档(https://edgeguides.rubyonrails.org/active_storage_overview.html)中写的所有事情。它也可以正常工作,将图像内容写入数据库,但是当我尝试显示图片时,它只是向我显示“无法加载图形图标”。
这里是我的代码:
artwork.rb
class Artwork < ApplicationRecord
THUMBNAIL_SIZE = '150x150'
has_one_attached :image
end
artworks_controller.rb
class ArtworksController < ApplicationControlle
def create
return redirect_to({:controller => :exhibitions, :action => :show, :id => @artwork.exhibition_id}, :notice => 'Kunstwerk wurde erfolgreich erstellt') if @artwork.save
render :action => :form
end
def update
return redirect_to({:controller => :exhibitions, :action => :show, :id => @artwork.exhibition_id}, :notice => 'Kunstwerk wurde erfolgreich aktualisiert') if @artwork.update(artwork_params)
render :action => :form
end
private
# Use callbacks to share common setup or constraints between actions.
def set_artwork
@artwork ||= Artwork.find(params[:id])
end
def new_artwork
return redirect_to({:controller => :exhibitions, :action => :index}) if params[:artwork].blank? || params[:artwork][:exhibition_id].blank?
@artwork ||= Artwork.new(artwork_params)
end
# Only allow a trusted parameter "white list" through.
def artwork_params
params.require(:artwork).permit(:title, :description, :designation, :size, :time, :technique, :room_id, :artist_id, :exhibition_id, :inventory_number, :image)
end
end
show.html.erb
<%= image_tag(artwork.image) %>
environments / development.rb
config.active_storage.service = :local
config / storage.yml
local:
service: Disk
root: <%= Rails.root.join("storage") %>
我认为您想要的是主动存储
<%= image_tag url_for(artwork.image) %>