用户创建个人资料并上传头像....没关系。但随后用户决定添加更多信息,以便第二次访问配置文件设置并仅更改一个或两个文本字段但不更改头像。 但保存更改后,头像消失了。如果它没有改变,我希望它留在那里。
验证,如果未选择头像,则参数不允许,似乎没有任何帮助。
编辑:添加一些代码
blog.rb模型:
mount_uploader :blog_title_img, BlogTitleUploader
blog_title_uploader.rb:
class BlogTitleUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
def extension_white_list
%w(jpg jpeg gif png)
end
storage :file
kvalita = 90
def store_dir
"uploads/#{model.class.to_s.underscore}/#{model.id}/#{mounted_as}"
end
version :thumb464 do
process :resize_to_fill => [464,304]
process :convert => 'jpg'
process :quality => kvalita
end
version :thumb340, from_version: :thumb464 do
process :resize_to_fill => [340,304]
process :convert => 'jpg'
process :quality => kvalita
end
version :thumb320, from_version: :thumb464 do
process :resize_to_fill => [320,200]
process :convert => 'jpg'
process :quality => kvalita
end
version :thumb288, from_version: :thumb464 do
process :resize_to_fill => [288,304]
process :convert => 'jpg'
process :quality => kvalita
end
version :thumb256, from_version: :thumb464 do
process :resize_to_fill => [256,304]
process :convert => 'jpg'
process :quality => kvalita
end
version :thumb224, from_version: :thumb464 do
process :resize_to_fill => [224,304]
process :convert => 'jpg'
process :quality => kvalita
end
def filename
"#{secure_token()}.jpg"
end
def secure_token(length=16)
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length/2))
end
end
博客/ edit.html.erb:
<%=
render partial: "shared/editor",
object: @blog,
locals: {
backlink: @backlink,
title_image: :blog_title_img
}
%>
共享/ _editor.html.erb:
<%= form_for editor do |f| %>
<b>Nadpis</b>
<br>
<%= f.text_field :title, class:"nadpis" %>
<br>
<b>Link</b>
<br>
<%= f.text_field :link, class:"nadpis" %>
<% if defined?(editor.tags) %>
<br>
<b>Tagy</b>
<br>
<%= f.text_field :tags, class:"nadpis" %>
<% end %>
<br>
<b>Úvodný text / popis</b>
<br>
<%= f.text_area :description, class:"nadpis" %>
<br>
<b>Text</b>
<br>
<%= f.text_area :text, class:"text" %>
<br>
<b>Titulný obrázok</b>
<br>
<%= image_tag(editor.send(title_image).thumb320.url) if editor.send(title_image) != nil %>
<br>
<%= f.file_field title_image %>
<br>
<%= f.hidden_field title_image.to_s + "_cache" %>
<br>
<% if defined?(kategorie) #&& z.length > 0
z = kategorie.reflections[kategorie.reflections.keys.first].active_record.all
z.each do |ch| %>
<span>
<%= check_box "pica", :id, { multiple: true, checked: in_result_set(kategorie, ch) }, ch.id, nil %>
<%= ch.title %>
</span>
<% end %>
<br>
<% end %>
<%= f.radio_button :visible, true %> Publikovať po odoslaní
<br>
<%= f.radio_button :visible, false %> Zatiaľ nepublikovať
<br>
<br>
<%= f.hidden_field :typ, value: params[:typ] %>
<%= f.submit "Uložiť" %>
<% end %>
答案 0 :(得分:0)
对于有类似问题的未来访客,我终于找到了解决方案:
在blog_title_uploader.rb中更改:
def filename
"#{secure_token()}.jpg"
end
为:
def filename
"#{secure_token()}.jpg" if original_filename.present?
end
现在页面的行为符合要求。我应该尽量控制输出并分析它在幕后做的事情......
解决方案取自此处:Carrierwave filename keeps changing on update_attributes