【问题标题】:Attachment_fu failing to validate on updateAttachment_fu 更新验证失败
【发布时间】:2009-08-18 14:56:17
【问题描述】:

我已经在一个项目上使用 attachment_fu 很长时间了,一切都很好,但现在我正试图将项目升级到 rails 2.3.3,我遇到了一个让我发疯的奇怪错误。附件(在这种情况下是一个徽标)在创建时正确验证,但在更新时不会失败验证。我已经对其进行了调试,但它未能通过初始验证,但似乎没有引发异常,或者至少没有引发我在控制器中的救援捕获的异常。好像我已经尝试了一切,但无法弄清楚这一点。

控制器:

  # POST /tournaments
  # POST /tournaments.xml
  def create
    # Build tournament
    @tournament = Tournament.new(params[:tournament].merge(:user_id => current_user.id) )

    # Save the uploaded attachments
    params[:uploads].each do |upload|
      @tournament.documents << Document.new({:uploaded_data => upload[:document]}.merge(:description => upload[:description]))
    end unless params[:uploads].nil?

    # if supplied save an event logo
    @logo = Logo.new({:uploaded_data => params[:logo][:upload_data]}) unless params[:logo].nil? or params[:logo][:upload_data].blank?  
    @tournament.logo = @logo unless @logo.nil?     

    respond_to do |format|
      begin
        Tournament.transaction do    
          @tournament.logo.save! unless @tournament.logo.nil?
          @tournament.save!  
        end
        flash[:notice] = 'Tournament was successfully created.'
        format.html { redirect_to tournament_url(@tournament) }
        format.xml  { head :created, :location => tournament_url(@tournament) }        
      rescue 
          flash[:notice] = 'Errors prevented your Tournament from being saved'
        format.html { render :action => "new" }
        format.xml  { render :xml => @tournament.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /tournaments/1
  # PUT /tournaments/1.xml
  def update
    @tournament = Tournament.find(params[:id])
    @tournament.user_id = session[:orig_user_id]

    respond_to do |format|
      begin
        Tournament.transaction do   
          # Update Logo if necessary
          unless params[:logo][:upload_data].blank?
            @tournament.logo.destroy unless @tournament.logo.nil?
            @tournament.logo = Logo.new({:uploaded_data => params[:logo][:upload_data]}.merge(:user_id => current_user.id)) 
          end   
          # Save any uploaded documents
          params[:uploads].each do |upload|
            @tournament.documents << Document.new({:uploaded_data => upload[:document]}.merge(:description => upload[:description]))
          end unless params[:uploads].nil?   
          # Update Tournamnet Attributes
          @tournament.attributes = params[:tournament]  
          # Save the Tournament
          @tournament.save!         
        end
        flash[:notice] = 'Tournament was successfully updated.' 
        format.html { redirect_to tournament_url(@tournament) }
        format.xml  { head :ok, :location => tournament_url(@tournament) }        
      rescue 
          flash[:notice] = 'Errors prevented your Tournament from being updated' 
        format.html { render :action => "edit" }
        format.xml  { render :xml => @tournament.errors, :status => :unprocessable_entity }
      end
    end
  end  

标志模型:

    class Logo < Asset    

  validate_on_create :attachment_valid? 

  has_attachment :content_type => :image, 
    :storage => :file_system, 
    :max_size => 4.megabytes,
    :resize_to => '810x150>',  
    :processor  => :ImageScience,
    :thumbnails => { :thumb => '270x50>'  }  


  def attachment_valid? 
    content_type = attachment_options[:content_type] 
    unless content_type.nil? || content_type.include?(self.content_type) 
      errors.add(:upload_data, " * must be an image file (jpg, gif, or png)")  
    end 
    size = attachment_options[:size] 
    unless size.nil? || size.include?(self.size)               
      errors.add(:upload_data, "* image must be 4MB or less")  
    end   
  end 


  before_thumbnail_saved do |thumbnail|
    record = thumbnail.parent
    thumbnail.user_id = record.user_id 
    thumbnail.listing_id = record.listing_id
  end

end     

我正在执行以下操作

Rails 2.3.3

image_science 1.2.0

谢谢 --蒂姆

【问题讨论】:

    标签: ruby-on-rails attachment-fu


    【解决方案1】:

    您还可以使用 :before_save 回调来测试对象。如果无效,则引发异常。

    【讨论】:

    • 这似乎是最好的方法
    • 通过在我的自定义验证中引发 RecordInvalid 异常并将徽标分配到 tournamnet 到事务块中,我能够使其正常工作。感谢您的帮助。
    【解决方案2】:

    尝试添加:

    validate_on_update :attachment_valid?
    

    【讨论】:

      猜你喜欢
      • 2018-01-31
      • 2014-11-01
      • 1970-01-01
      • 2019-02-26
      • 2014-04-23
      • 1970-01-01
      • 2017-03-08
      • 2014-05-08
      • 2017-07-25
      相关资源
      最近更新 更多