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"

为什么图像类不只是“覆盖”旧图像并上传新图像,就像它应该做的那样?

4

1 回答 1

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

于 2013-04-04T19:11:58.850 回答