Created
March 8, 2016 13:23
-
-
Save Godoy/582796f058be306fdb9b to your computer and use it in GitHub Desktop.
envia formulário ruby on rails via ajax e exibe erros automaticamente com script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%= form_for(@model, remote: true, html: {role: :form, 'data-model' => 'MODEL_NAME', class: 'send_ajax'}) do |f| %> | |
| <% if @model.errors.any? %> | |
| <div id="error_explanation"> | |
| <h2><%= pluralize(@model.errors.count, "error") %> prohibited this tip from being saved:</h2> | |
| <ul> | |
| <% @model.errors.full_messages.each do |message| %> | |
| <li><%= message %></li> | |
| <% end %> | |
| </ul> | |
| </div> | |
| <% end %> | |
| <div class="status" style="display:none;">Success message!</div> | |
| <div class="row"> | |
| <div class="col-sm-12"> | |
| <%= f.text_area :explanation, rows: 5, placeholder: "How are you?" %> | |
| <span class="help-block"></span> | |
| </div> | |
| </div> | |
| <div class="row"> | |
| <div class="col-sm-6"> | |
| <%= f.text_field :name, placeholder: "Your name" %> | |
| <span class="help-block"></span> | |
| </div> | |
| <div class="col-sm-6"> | |
| <%= f.text_field :email, placeholder: "Your email" %> | |
| <span class="help-block"></span> | |
| </div> | |
| </div> | |
| <div class="row"> | |
| <div class="col-sm-12 text-center"> | |
| <%= f.submit "Submit" %> | |
| </div> | |
| </div> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(document).ready(function(){ | |
| $(document).bind('ajaxError', 'form.send_ajax', function(event, jqxhr, settings, exception){ | |
| $(event.data).render_form_errors( $.parseJSON(jqxhr.responseText) ); | |
| }) | |
| .bind('ajaxSuccess', 'form.send_ajax', function(event, jqxhr, settings, exception){ | |
| $(event.data).clear_form(); | |
| $(event.data).success() | |
| }); | |
| }); | |
| (function($) { | |
| $.fn.clear_form = function(){ | |
| // clear form input elements | |
| this.find('input[type="text"]').val(''); | |
| this.find('textarea').val(''); | |
| // clear error state | |
| this.clear_previous_errors(); | |
| }; | |
| $.fn.render_form_errors = function(errors){ | |
| // $form = this; | |
| this.clear_previous_errors(); | |
| model = this.data('model'); | |
| // show error messages in input form-group help-block | |
| $.each(errors, function(field, messages){ | |
| $input = $('[name="' + model + '[' + field + ']"]'); | |
| $input.closest('div').addClass('has-error').find('.help-block').html( messages.join(' & ') ); | |
| }); | |
| }; | |
| $.fn.clear_previous_errors = function(){ | |
| $('.has-error', this).each(function(){ | |
| $('.help-block', $(this)).html(''); | |
| $(this).removeClass('has-error'); | |
| }); | |
| } | |
| $.fn.success = function() { | |
| $('.status', this).show(); | |
| } | |
| }(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment