Drextor Drextor - 3 months ago 22
Ruby Question

Ruby on Rails - AbstractController::DoubleRenderError

i try to add to my create-method one redirect to my support tickets. But always i get an


AbstractController::DoubleRenderError


But i need this redirect after the upload of the file. How can i fix this ?

Upload-controller :

def create

@upload = Upload.new(params[:upload])

respond_to do |format|
if @upload.save
format.html {
render :json => [@upload.to_jq_upload].to_json,
:content_type => 'text/html',
:layout => false
}

format.json { render json: {files: [@upload.to_jq_upload]}, status: :created, location: @upload }

else
format.html { render action: "new" }
format.json { render json: @upload.errors, status: :unprocessable_entity }
end
redirect_to new_support_ticket_path
end
end


upload.rb :

class Upload < ActiveRecord::Base
attr_accessible :upload
has_attached_file :upload

include Rails.application.routes.url_helpers

def to_jq_upload
{
"name" => read_attribute(:upload_file_name),
"size" => read_attribute(:upload_file_size),
"url" => upload.url(:original),
"delete_url" => upload_path(self),
"delete_type" => "DELETE"
}

end

end

Answer

Because it doesn't allow you to redirect_to after render. I think redirect_to new_support_ticket_path should be placed in format.html block because you need to redirect_to in html view.

@upload = Upload.new(params[:upload])

  respond_to do |format|
      if @upload.save
       format.html {
         redirect_to new_support_ticket_path
       }

       format.json { render json: {files: [@upload.to_jq_upload]}, status: :created, location: @upload }

    else
       format.html { render action: "new" }
       format.json { render json: @upload.errors, status: :unprocessable_entity }
    end
 end

end