ruby on rails - carrierwave deletes user avatar on user profile update -
ruby on rails - carrierwave deletes user avatar on user profile update -
user creates profile , upload avatar.... that's ok. user decide add together more info visit profile settings sec time , alter 1 or 2 text fields no avatar. after saving changes avatar disappears. want remain there if not changed.
validations, parameters disallow if avatar not selected, nil seems help.
edit: adding code
blog.rb model:
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 process :resize_to_fill => [464,304] process :convert => 'jpg' process :quality => kvalita end version :thumb340, from_version: :thumb464 process :resize_to_fill => [340,304] process :convert => 'jpg' process :quality => kvalita end version :thumb320, from_version: :thumb464 process :resize_to_fill => [320,200] process :convert => 'jpg' process :quality => kvalita end version :thumb288, from_version: :thumb464 process :resize_to_fill => [288,304] process :convert => 'jpg' process :quality => kvalita end version :thumb256, from_version: :thumb464 process :resize_to_fill => [256,304] process :convert => 'jpg' process :quality => kvalita end version :thumb224, from_version: :thumb464 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
blogs/edit.html.erb:
<%= render partial: "shared/editor", object: @blog, locals: { backlink: @backlink, title_image: :blog_title_img } %>
shared/_editor.html.erb:
<%= form_for editor |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 |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 %>
ok future visitors similar problem found solution:
in blog_title_uploader.rb change:
def filename "#{secure_token()}.jpg" end
to:
def filename "#{secure_token()}.jpg" if original_filename.present? end
now page behaves desired. should seek console output , analyze doing under hood sooner...
solution taken here: carrierwave filename keeps changing on update_attributes
ruby-on-rails carrierwave
Comments
Post a Comment