Hello again

I am trying to show the errors that can occur by uploading a file (file name, size).
I tried to modify this code cut-off but I could not.

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

I have following validations and I want json to write when an error occured, so something like this:

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

(it doenst work) and I need a specification of an error, that is written in a validation :message

my validations:

validates :upload_file_name,  :presence   => true,
                                :format     =>{:with => %r{\.(cel)$}i}

  validates_uniqueness_of :upload_file_name, :message => "File with this name is already in the database"

  validates :upload_file_size,  :inclusion  => {:in =>10.megabytes..20.megabytes}, :message =>"Too big or too small"

  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

Thanks in advanced

Member Avatar for LastMitch

I am trying to show the errors that can occur by uploading a file (file name, size).

I'm not sure what you are asking.

You never mention what uploading file you are using.

Plus I got no idea what language for the upload file you are using.

You can try this is and see if the message appears:

else
format.html { render action: "new" }
render json: {error: @json.upload.errors.full_messages}, status: :unprocessable_entity 
end
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.