我要显示上传图片
但是,我似乎无法获取图片元数据或缺少图片路径,
我尝试了一些方法来更改img代码和CSS,更改了模型实例,
更改img代码:
之前:
<div class="image-preview">
<img id="image" src="<%= photos_fileds.object.image_url(:medium) %> class="rounded" >
</div>
之后:
<%= image_tag (photos_fileds.object.image_url(:medium)),id: "image",class:"rounded cash-image show-
image" %>
</div>
Css:
之前:
img {
display: block;
/* This rule is very important,please don't ignore this */
max-width: 100%;
}
.image-preview img {
display: block;
max-width: 100%;
}
.image-preview {
margin-bottom: 10px;
display: inline-block;
height: 300px;
}
之后:
.show-image{
display: block;
/* This rule is very important,please don't ignore this */
max-width: 100%;
}
.image-preview > cash-image {
display: block;
max-width: 100%;
}
.image-preview {
margin-bottom: 10px;
display: inline-block;
height: 300px;
}
更改型号:
之前:
<%= f.fields_for :photos,Photo.new do | photos_fields| %>
<%= photos_fileds.label :image %>
<%= photos_fileds.hidden_field :image,value: photos_fileds.object.cached_image_data %>
之后:我使用了临时照片模型,因为我必须出现photos_fields,但是我在 形状对象
<%= f.fields_for :photos,@blog_form.get_photos do | photos_fields| %>
....
这些是完整的代码
Shrine.rb:
require "shrine"
require "shrine/storage/file_system"
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new("app/assets/images",prefix: "uploads/cache"),# temporary
store: Shrine::Storage::FileSystem.new("app/assets/images",prefix: "uploads"),# permanent
}
Shrine.plugin :activerecord # loads active Record integration
Shrine.plugin :cached_attachment_data # enables retaining cached file across form redisplays
Shrine.plugin :restore_cached_data # extracts metadata for assigned cached files
Shrine.plugin :determine_mime_type
Shrine.plugin :derivatives
Shrine.plugin :backgrounding
Shrine::Attacher.promote_block { PromoteJob.perform_later(record,name,file_data) }
Shrine::Attacher.destroy_block { DestroyJob.perform_later(data) }
Shrine.plugin :derivation_endpoint,secret_key: Rails.application.secret_key_base
Shrine.plugin :default_url
BlogForm.rb:
class BlogForm
include activeModel::Model
attr_accessor :title,:content,:user_id,:photos_attributes
def get_blog
@blogs ||= Blog.new
end
def get_photos
@photos ||= Photo.new
end
def blog_builder
@user = User.find(user_id)
@blogs = @user.blogs.create(title: title,content: content )
end
concerning :PhotosBuilder do
attr_reader :photos_attributes
def photos
@photos ||= Photo.new
end
def photos_attributes=(attributes)
@photos ||= Photo.new(attributes)
end
end
def build_association
@blogs.photos << @photos if photos?
@user.photos << @photos if photos
end
def save
return false if invalid?
blog_builder
@blogs.save
@photos.save if @photos != nil
build_association
end
def photos?
return true if @photos != nil
end
end
Form.html.erb:
<div class ="row">
<%= form_with model: @blog_form,url: user_blogs_path,local: true do |f| %>
<div class ="field ">
<%= f.label :title,class: "col-sm-12 col-10" %><br />
<%= f.text_field :title,class: "col-sm-12 col-10"%><br />
</div>
<div class ="field">
<%= f.label :content %><br />
<%= f.rich_text_area :content %><br />
<%= @blog_form.get_blog.content %>
</div>
<%= f.fields_for :photos,@blog_form.get_photos do | photos_fields| %>
<%= photos_fields.label :image %>
<%= photos_fields.hidden_field :image,value: photos_fields.object.cached_image_data %>
<div class="image-preview">
<%= image_tag (photos_fields.object.image_url),class:"rounded cash-image show-
image" %>
</div>
<%= photos_fields.file_field :image %>
<div class="image-preview">
<img id="image" src="<%= photos_fields.object.image_url(:medium) %> class="rounded" >
</div>
<% end %>
如果您熟悉神社,您想帮我吗?