2009-08-18 28 views
1

私は長い間プロジェクトにattachment_fuを使用していましたが、すべて問題ありませんでしたが、現在はプロジェクトをレール2.3.3に持っていこうとしています。私をナットにしている。添付ファイル、この場合のロゴは、作成時に正しく検証されますが、更新時に検証に失敗しません。私はそれをデバッグして、それはintial検証に失敗しますが、例外をスローしていないか、少なくとも私のレスキューでコントローラにキャッチされているものはありません。私はすべてを試したようだが、これを理解することはできない。Attachment_fuが更新時に検証に失敗しました

コントローラー:

# 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

T

validate_on_update :attachment_valid? 

答えて

1

ます。また、使用することができます:オブジェクトをテストするためにbefore_saveコールバックを追加するハンクス --Tim

+0

と呼ばれることはありません。これは最善の方法です – triendeau

+0

私のカスタム検証でRecordInvalid例外を発生させ、ロゴの割り当てをtournamnetのトランザクションブロックに移動することで、この作業を行うことができました。助けてくれてありがとう。 – triendeau

0

試してみます。無効な場合は、例外を発生させます。

+0

これは決して – triendeau

関連する問題