Go Back   CodingForums.com > :: Server side development > Ruby & Ruby On Rails

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-28-2012, 08:53 PM   PM User | #1
akvarel
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
akvarel is an unknown quantity at this point
How can I associate error messages to the 'error' key in json response

I am trying to show the errors that can occur by uploading a file (file name, size).
I am using an adapted version of Jquery Fiel Upload for Ruby on Rails

I need to have my error messages (validations) associated to the 'error' key in my json response.

I guess I have to change the javascript to make it work.


json:

Code:
 render json: {error: @upload.errors.full_messages}, status: :unprocessable_entity
validations:

Code:
     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"
Javascript I use to upload the files:

Code:
<script type="text/javascript" charset="utf-8">
          $(function () {
              // Initialize the jQuery File Upload widget:
              $('#fileupload').fileupload();
              // 
              // Load existing files:
              $.getJSON($('#fileupload').prop('action'), function (files) {
                var fu = $('#fileupload').data('fileupload'), 
                  template;
                fu._adjustMaxNumberOfFiles(-files.length);
                console.log(files);
                template = fu._renderDownload(files)
                  .appendTo($('#fileupload .files'));
                // Force reflow:
                fu._reflow = fu._transition && template.length &&
                  template[0].offsetWidth;
                template.addClass('in');
                $('#loading').remove();
              });
        
          });
        </script>
I have tried as well those things:
`json: => [{:error=> @upload.errors.full_messages }] }`
using this shows only the validation message but I need the status: as well

I was suggested as well to do this way. But I dont have a js.file, the only javascript I have is in the index.html.erb and you can see it above.

Quote:
Code:
render :json => { :errors => @example.errors.full_messages }, :status => 422
in your js file:

Code:
$('.form_id_or_class').live('submit', function() {
      $.ajax({
        url: $(this).attr("action"),
          type: "POST",
          data: $(this).serialize(),
          success: function(data) {
           // do some action
        }
        ,error: function(xhr){
          var errors = $.parseJSON(xhr.responseText).errors
        }
      });
      return false;
    });
Many thanks in advanced
akvarel is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:01 AM.


Advertisement
Log in to turn off these ads.