Hello I am trying to show the errors that can occur by uploading a file (file name, size). I need have your error messages associated to the ''error'' key in my json response else format.html { render action: "new" } format.json { render json: @upload.errors, status: :unprocessable_entity } end my validations: 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 I have tried: format.json { render json: => [{:error=> @upload.errors.full_messages }] } using this shows the validation message but I need the status: to be there as well render json: {error: @upload.errors.full_messages}, status: :unprocessable_entity shows no error messages at all render json: {error: @upload.errors.full_messages}, status: :unprocessable_entity I get an error-text : Internal Server Error and not what stands in my validation messages Thanks in advanced -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/tbZoal6I0UgJ. For more options, visit https://groups.google.com/groups/opt_out.
in your controller: render :json => { :errors => @example.errors.full_messages }, :status => 422 in your js file: $(''.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; }); On Sun, Oct 28, 2012 at 1:37 AM, Akvarel <kate.kokatjuhha-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello > I am trying to show the errors that can occur by uploading a file (file > name, size). > I need have your error messages associated to the ''error'' key in my json > response > > else > format.html { render action: "new" } > format.json { render json: @upload.errors, status: > :unprocessable_entity } > end > > > my validations: > > > 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 > > > I have tried: > format.json { render json: => [{:error=> @upload.errors.full_messages }] } > using this shows the validation message but I need the status: to be there > as well > > render json: {error: @upload.errors.full_messages}, status: > :unprocessable_entity > shows no error messages at all > > > render json: {error: @upload.errors.full_messages}, status: :unprocessable_entity > I get an error-text : Internal Server Error and not what stands in my validation messages > > Thanks in advanced > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/tbZoal6I0UgJ. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- Regards by Saravanan.P -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Sun, Oct 28, 2012 at 12:26 AM, Saravanan P <saravanan.p-tks5Z7IV8F8RmelmmXo44Q@public.gmane.org> wrote:> render :json => { :errors => @example.errors.full_messages }, :status => 422Why are you sending a 422? If you really want to stick in the 400 range it would be more suitable to send a 409 conflict since it literally is a conflict but some just prefer to stick to a generic 500. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
> > in your js file: > > $(''.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; > }); >The only js script i have there is the following and it is in the index.html.erb. I am using this example for File uploader in Ruby on Rails<https://github.com/tors/jquery-fileupload-rails-paperclip-example> . Should I just add your code to mine or mine code has to be modified? <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> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Sunday, 28 October 2012 02:07:27 UTC-4, Jordon Bedwell wrote:> > On Sun, Oct 28, 2012 at 12:26 AM, Saravanan P > <sarav...-tks5Z7IV8F8RmelmmXo44Q@public.gmane.org <javascript:>> wrote: > > render :json => { :errors => @example.errors.full_messages }, :status => > 422 > > Why are you sending a 422? If you really want to stick in the 400 > range it would be more suitable to send a 409 conflict since it > literally is a conflict but some just prefer to stick to a generic > 500. >422 is the recommended behavior from respond_with and friends: https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/responder.rb#L259 409 seems odd, given that the error could be "file too large" which isn''t really "a conflict with the current state of the resource" as described in the RFC. Also note that returning a 422 causes ActiveResource to behave in an expected manner (failing a save rather than raising an exception), which may-or-may-not be relevant for this use case. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/caglVv5ftUsJ. For more options, visit https://groups.google.com/groups/opt_out.