2014-06-21 99 views
-5

用戶創建配置文件並上傳頭像....沒關係。但是,用戶決定添加更多信息,以便第二次訪問配置文件設置,並且只更改一個或兩個文本字段,但不更改頭像。 但保存更改頭像消失後。如果它沒有改變,我希望它留在那裏。carrierwave刪除用戶配置文件更新的用戶頭像

驗證,參數不允許如果沒有選擇頭像,沒有什麼似乎幫助。

編輯:加入一些代碼

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 %> 
+3

一些代碼會有所幫助。 您是否正在安裝上載器以進行活動記錄或使用您自己的配置? –

回答

0

OK用類似的問題今後遊客我終於找到了解決辦法:

在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