2013-04-04 38 views
1

我有一個設置,我有一個圖像類,我有一個ImageUploader。我將該類嵌入到幾個不同的其他類中,並使用嵌套形式向其上傳圖像。用載波和嵌套表格重新上傳圖像

一切都很好,除非一旦我上傳了一張圖片,我無法再重新上傳。所以我不能按上傳按鈕,並選擇另一個圖像,就像通常可以使用載波一樣。

圖像類:

class Image 
    # 1. Include mongoid stuff 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    # 2. Mount uploaders 
    mount_uploader :image, ImageUploader 

    # 3. Define fields 
    field :y, type: String, default: "Test" 
    field :image 
    field :remove_image 

    # 4. Set them accessible 
    attr_accessible :y, :image, :image_url, :image_cache, :remove_image, :image_cover_url 

    # 5. Set associations 
    embedded_in :imageable, polymorphic: true 
end 

第類別:

class Article 
    # 1. Include mongoid stuff 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    include Mongoid::Slug 

    # 2. Define fields 
    field :title, type: String 
    field :lead, type: String 
    field :main_text, type: String 

    # 3. Set attributes accesible 
    attr_accessible :title, :lead, :main_text, :article_image_attributes 

    # 4. Set slug 
    slug :title 

    # 5. Set associations 
    belongs_to :issue 
    embeds_one :article_image, :as => :imageable, :class_name => 'Image', cascade_callbacks: true 

    # 6. Accepting nested attributes 
    accepts_nested_attributes_for :article_image, :allow_destroy => true 

    # 7. Set validations 
    validates_presence_of :title, :lead, :main_text 
    # validate :validate_minimum_article_image_size 

    def validate_minimum_article_image_size 
    geometry = self.article_image.geometry 
    unless geometry.nil? 
     width = geometry[0] 
     height = geometry[1] 

     unless (width >= 1536 && height >= 850) 
     errors.add :base, "Article image should minimum be 1536px wide and 850px tall, but was "+width.to_s+"px wide and "+height.to_s+"px tall." 
     end 
    end 
    end 
end 

第控制器:

class ArticlesController < ApplicationController 
    # 1. Include helpers 
    include ApplicationHelper 
    include ArticlesHelper 

    # 2. Set before filters 
    before_filter :authenticate_user!, :unless => :format_whitelist?, :except => [:show] 
    before_filter :verify_user 
    before_filter :set_objects 
    before_filter :set_article 

    # 3. Set the responders 
    respond_to :html 

    # 4. Define and set controllers 
    def show 
    end 

    def new 
    @article = @issue.articles.new 
    @article.build_article_image 
    end 

    def edit 
    @article.build_article_image unless @article.article_image.present? 
    end 

    def create 
    @article = @issue.articles.new(params[:article]) 

    if @article.save 
     redirect_to magazine_issue_path(@magazine, @issue), notice: 'Article was successfully created.' 
    else 
     render action: "new", alert: add_errors(@article) 
    end 
    end 

    def update 
    if @article.update_attributes(params[:article]) 
     redirect_to magazine_issue_path(@magazine, @issue), notice: 'Article was successfully updated.' 
    else 
     render action: "edit", alert: add_errors(@article) 
    end 
    end 

    def destroy 
    @article.destroy 
    redirect_to :back, notice: 'Article was successfully deleted.' 
    end 
end 

文章形式:

= nested_form_for [@magazine, @issue, @article], :validate => true do |f| 
    %div.center_big.input_area 
    = f.text_field :title, :placeholder => "Title of Article" 
    = f.text_area :lead, :placeholder => "Articles leading text", :class => "lead" 
    = f.fields_for :article_image do |i| 
     %div.article_image_box.image_box 
     = i.link_to_remove "Remove image", :class => "remove_image" 
     %span.size.center_big.light.italic Min width and height is 1536px - 850px 
     %div.article_image.image 
      = image_tag(i.object.image_url) if i.object.image? 
     %div.button.grey 
      Choose Article Image (optional) 
      = i.file_field :image, :class => "button upload_button" 
      = i.hidden_field :image_cache 
    = f.text_area :main_text, :placeholder => "Articles body text", :class => "redactorbox" 
    %div= f.submit "Save Article", :disable_with => "Saving...", :class => "button" 

爲什麼沒有圖像類只是「覆蓋」舊的圖像,並上傳新的圖片,喜歡它的咋辦?

回答

2

在我的更新函數中,我不得不指定我想更新我的article_image_attributes。

所以不是:

if @article.update_attributes(params[:article]) 

我不得不這樣做:

if @article.update_attributes(params[:article]) && @article.article_image.update_attributes(params[:article][:article_image_attributes]) 

我無法找到任何博客或教程這些信息,所以爲人民投票這個問題倒,如果你能幫忙找到答案,那就太好了。我相信其他人也試圖遇到同樣的問題。

編輯:

好像我不會有這個問題只有一個 - https://github.com/jnicklas/carrierwave-mongoid/pull/64

+0

不知道爲什麼這是downvoted,這正是我所需要的修復。顯然,如果沒有使用參數顯式更新現有圖像,表單驗證後完全忽略了圖像的緩存值。 +1 – DemitryT 2015-02-27 11:42:21