我有一个设置,我有一个图像类,我有一个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"
为什么图像类不会“覆盖”旧图像并上传新图像,就像它应该的那样?
答案 0 :(得分: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。