Ruby on rails 编辑以前成功上载图像的记录时,如何不必在编辑时重新上载?
我使用Carrierwave、Fog存储上传到S3的图像 问题是,当我去编辑记录时,“文件”和“照片”字段会自动变为空白。因此,如果我想保留记录中的图像或文件,我必须重新上传它 否则,上传的文件/图像就会消失。我甚至不确定它是否从S3中被删除,但与我在db中的记录的关联消失了 这是我的SimpleFormRuby on rails 编辑以前成功上载图像的记录时,如何不必在编辑时重新上载?,ruby-on-rails,ruby-on-rails-4,amazon-s3,carrierwave,fog,Ruby On Rails,Ruby On Rails 4,Amazon S3,Carrierwave,Fog,我使用Carrierwave、Fog存储上传到S3的图像 问题是,当我去编辑记录时,“文件”和“照片”字段会自动变为空白。因此,如果我想保留记录中的图像或文件,我必须重新上传它 否则,上传的文件/图像就会消失。我甚至不确定它是否从S3中被删除,但与我在db中的记录的关联消失了 这是我的SimpleForm\u form.html.erb部分,用于我的Post模型: <%= simple_form_for(@post, html: {class: 'form-horizontal' }) d
\u form.html.erb
部分,用于我的Post
模型:
<%= simple_form_for(@post, html: {class: 'form-horizontal' }) do |f| %>
<%= f.error_notification %>
<%= f.input_field :title, placeholder: "Enter Title" %>
<%= f.input_field :body, id: "body-field", placeholder: "Provide all the facts." %>
<%= f.input_field :photo %>
<%= f.input_field :file %>
<%= f.button :submit, class: "btn btn-primary pull-left" %>
<% end %>
这是我的FileUploader
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :fog
include CarrierWave::MimeTypes
process :set_content_type
def store_dir
"images/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :main_thumb_mobile do
process :resize_to_fit => [52, 52]
end
version :main_thumb do
process :resize_to_fit => [150, 150]
end
version :post_thumb do
process :resize_to_fit => [200, 200]
end
version :large do
process :resize_to_limit => [400, 400]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
class FileUploader < CarrierWave::Uploader::Base
storage :fog
def store_dir
"files/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(pdf doc docx xls xlsx ppt pptx txt mp4 m4v mov avi mkv mp3 wav)
end
end
编辑1
这是我的Post.rb
:
# == Schema Information
#
# Table name: posts
#
# id :integer not null, primary key
# title :text
# photo :string(255)
# body :text
# created_at :datetime
# updated_at :datetime
# user_id :integer
# ancestry :string(255)
# file :string(255)
# status :integer default(0)
# slug :string(255)
# publication_status :integer default(0)
# has_eyewitness :boolean default(FALSE)
# youtube_embed_code :text
# soundcloud_embed_code :text
#
class Post < ActiveRecord::Base
has_ancestry
belongs_to :user
resourcify
enum status: [ :unconfirmed, :corroborated, :confirmed ]
enum publication_status: [ :unpublished, :published ]
extend FriendlyId
friendly_id :title, use: [:slugged, :history, :finders]
attr_accessor :country
mount_uploader :photo, PhotoUploader
mount_uploader :file, FileUploader
validates_presence_of :body
validates_length_of :body, maximum: 150, too_long: 'The report must be less than 150 words.',
tokenizer: ->(str) { str.scan(/\w+/) }
validates_length_of :title, maximum: 7, too_long: 'The title must be less than 7 words.',
tokenizer: ->(str) { str.scan(/\w+/) }
def publish
published!
end
def unpublish
unpublished!
end
def is_published?
if self.published?
"yes"
else
"no"
end
end
end
编辑3
PostController#Update
如下所示:
class PostsController < ApplicationController
load_and_authorize_resource
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Report was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
class PostsController
编辑4
您需要使用上传图像的输入字段来显示图像名称,并使用javascript函数在输入字段中显示文件名,如下所示:
<%= text_field_tag 'file_name', "#{@post.photo_file_name}", {disabled: true, class: 'browse-input form-control'} %>
<%= f.input_field :photo %>
:javascript
$("#post_photo").change(function () {
$("#file_name").val(this.value.replace(/^.*[\\\/]/, ''));
});
def update
if @post.photo && !@post.photo.changed?
@post.update(post_update_params_without_photo)
end
end
# Example method, don't forgot set the necessary attributes
def post_update_params_without_photo
params.require(:post).permit(:title)
end
解释:此代码仅用于更新更改的属性,即(标题、状态等)。我认为简单的表单是在制作参数时做一些额外的工作。当未选择任何文件时,它可能以
'post'=>{'file'=>{'id'=>'29','image'=>'''}}
的形式发送参数
然而,如果您尝试使用一个简单的file\u field\u标记
或f.file\u field
,那么如果没有选择图像文件,并且保留了旧的图像文件,那么它只会作为'post'=>{'file'=>{'id'=>'29'}
发送
试试这个,它肯定会解决你的问题
请记住,如果使用file\u field\u标记
,则需要正确传递name
属性,以便按照rails期望的正确方式构建参数。在您的情况下,它必须类似于'post[file][image],
和一个隐藏字段(如果使用f.file_字段
),其名称将是post[file][id]
共享我的控制台
于2015-01-05 10:44:53+0530为127.0.0.1启动补丁“/requirements/2162”
由RequirementsController处理#更新为HTML
参数:{“utf8”=>“✓", "真实性令牌“=>”mqoPYjGir/3aA/ZFCjpr79KY/xXEoyTd95Dis8HDH7g=“,”要求“=>”{“类别ID”=>[“40”,“47”],“标题”=>“gefd gefd gefd”,“详细信息”=>“dsfg”,“到期日(1i)”=>“2015”,“到期日(2i)”=>“12”,“到期日(3i)”=>“31”,“地址属性”=>“ewfg”、“城市”=>“聋人”、“国家代码”=>“01代码”,“id”=>“60”},“提交”=>“更新要求”,“id”=>“2162”}
用户加载(4.3ms)从“人”中选择“人”。“输入”(“用户”)和“人”。“id”=28按“人”排序。“id”ASC限制1
需求负载(1.0ms)从“需求”中选择“需求”,其中“需求”。`id`=2162限制1
(1.7毫秒)开始
类别加载(6.1ms)从(40,47)中的“类别”中选择“类别”。“id”
类别加载(5.0ms)从“类别”上的“类别”内部连接“类别”要求中选择“类别”。“id”=“类别”要求。“”类别id`其中“类别”要求。“”要求id`=2162
地址加载(0.4ms)从“地址”中选择“地址”。`id`=60限制1
DonorRequirement存在(0.4ms)从“捐赠者需求”中选择1作为一个,其中“捐赠者需求”。“需求id”=2162限制1
SQL(7.6ms)更新`requirements`设置`expiration\u date`='2015-12-31',`title`='gefd gefd',`updated\u at`='2015-01-05 05:14:53',其中`requirements`.`id`=2162
(6.0ms)提交我发现的最简单的解决方案是简单地添加一个检查以查看是否有照片附加,如
if@post.photo?
,如果是这样的话,只需显示一个标签即可
当然,这是假设我不想更改图像
<% if @post.photo? %>
<label><%= @post.photo.file.filename %></label>
<% else %>
<label for="photoFileUpload">Upload Images:</label>
<%= f.input_field :photo %>
<p class="help-block">Allowed: png, gif, jpg/jpeg</p>
<% end %>
上载图像:
允许:png、gif、jpg/jpeg
你能添加你的Post
模型的代码吗?看到日志后,我的想法完全改变了。当你请求补丁时,rails是如何触发插入查询的。它应该触发更新查询。我也请求共享你的控制器更新操作代码。现在检查路由指向何处。控制器操作看起来很好。只需运行“rake routes”并发送屏幕截图或文本。@Jaspret21并完成。刷新问题。嘿@marcamillion,问题在于触发的查询。我仔细检查了我的应用程序,当你更新任何记录时,它会在mysql中触发更新查询。与你的情况一样,它正在创建新记录!!:O正在触发插入查询。请尝试查看rst.1.检查是否创建了重复记录。2.如果发生了数字1,那么检查它为什么会发生。我对第一行有点困惑…它是注释还是应该是代码?应该是f.input
?这是标准ERB吗?我觉得不是。对不起,我只是想添加输入字段,但我忘记了er的语法b、 由于使用haml:):)我使用更新了我的答案,但这会阻止我的表单重新上传f.input\u字段:photo
?这似乎只是显示文件名,但不会重新附加数据库中已有记录的文件。换句话说,当用户已上传时一个文件,他们选择编辑记录,我希望表单显示该文件(在<代码中
def update
if @post.photo && !@post.photo.changed?
@post.update(post_update_params_without_photo)
end
end
# Example method, don't forgot set the necessary attributes
def post_update_params_without_photo
params.require(:post).permit(:title)
end
<% if @post.photo? %>
<label><%= @post.photo.file.filename %></label>
<% else %>
<label for="photoFileUpload">Upload Images:</label>
<%= f.input_field :photo %>
<p class="help-block">Allowed: png, gif, jpg/jpeg</p>
<% end %>