469,270 Members | 1,117 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

How to alert the response of controller to ajax using ruby on rails?

Exequiel
288 256MB
I can't output the response message of my controller into my ajax, the data is successfully save but it doesn't go to the success function in ajax.

heres the _form.html.erb
Expand|Select|Wrap|Line Numbers
  1. <%= form_for(@personnel, remote: true) do |e| -%>
  2.     <% if @personnel.errors.any? %>
  3.         <div class="alert alert-error">
  4.         <button type="button" class="close" data-dismiss="alert">&times;</button>
  5.             <h2>Errors</h2>
  6.             <ul>
  7.                 <% @personnel.errors.full_messages.each do |message| %>
  8.                     <li><%= message %></li>
  9.                 <% end %>
  10.             </ul>
  11.         </div>
  12.     <% end %>
  13.     <fieldset>
  14.         <div class="form-group">
  15.                 <label for="lastname">Last Name:</label><%= e.text_field :lastname -%>
  16.         </div>
  17.         <div class="form-group">
  18.             <label for="firstname">First Name:</label><%= e.text_field :firstname -%>
  19.         </div>
  20.         <div class="form-group">
  21.             <label for="middlename">Middle Name:</label> <%= e.text_field :middlename -%>
  22.         </div>
  23.         <div class="form-group">
  24.             <label for="extensionname">Extension Name:</label> <%= e.text_field :extensionname -%>
  25.             </div>
  26.         <%= e.submit 'Save', :class => 'btn btn-default', :id => 'save' -%>
  27.     </fieldset>
  28. <% end -%>
  29.  
heres my jquery ajax application.js
Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function(){
  2.  
  3.     $('#save').click(function(event){
  4.         event.preventDefault();
  5.  
  6.         $.ajax({
  7.             url: '/personnels.json',
  8.             type: 'POST',
  9.             success: function(a){
  10.                 alert(a.message);
  11.                 console.log(a.message);
  12.             }
  13.         });
  14.  
  15.     });
  16.  
  17.  
  18. });
  19.  
and here is my defined function create in controller personnels_controller.erb
Expand|Select|Wrap|Line Numbers
  1. def create
  2.         @personnel = Personnel.new(personnel_params)
  3.  
  4.         respond_to do |format|
  5.             if @personnel.save
  6.                 format.html { redirect_to @personnel }
  7.                 format.json { render json: { :status => 'Ok', :message => 'Personnel Successfully Added'}, status: :created }
  8.             else
  9.                 format.html { render action: 'new' }
  10.                 format.json { render json: @personnel.errors, status: :unprocessable_entity }
  11.             end
  12.         end
  13.     end
  14.  
  15.  
I did not show the entire controller only in my function create is the problem, , can anyone help me please? :) any comments are so appreciated, thank you very much. . :)
May 28 '14 #1

✓ answered by Exequiel

here are my changes in ajax.
Expand|Select|Wrap|Line Numbers
  1. $('#save').click(function(event){
  2.         event.preventDefault();
  3.         var data = $("form#new_personnel").serialize();
  4.  
  5.             $.ajax({
  6.                 url: '/personnels.json',
  7.                 type: 'POST',
  8.                 data: data,
  9.                 success: function(a){
  10.                     alert(a.message);
  11.                     console.log(a.message);
  12.                     checkform("form#new_personnel");
  13.  
  14.                     //window.location = '/personnels/' + a.id
  15.                 },
  16.                  error: function(request, status, error) {
  17.               //$(".error_output").html(request.responseText);
  18.                 errors = request.responseJSON;
  19.                 console.log(errors.lastname);
  20.  
  21.                 if(typeof errors.lastname !== 'undefined') {
  22.                     for(var error_message in errors.lastname) {
  23.                         $('#personnel_lastname_errors').append(error_message);    
  24.                     }    
  25.                 }
  26.             }
  27.             });
  28.  
  29.     });
  30.  
and here is my controller function create.
Expand|Select|Wrap|Line Numbers
  1. def create
  2.         @personnel = Personnel.new(personnel_params)
  3.  
  4.         respond_to do |format|
  5.             if @personnel.save
  6.                 format.html { render action: 'new' }
  7.                 format.json { render :json => { :status => 'Ok', :message => 'Personnel Successfully Added'}, :status => :created }
  8.             else
  9.                 format.html { render action: 'new' }
  10.                 format.json { render json: @personnel.errors, status: :unprocessable_entity }
  11.             end
  12.         end
  13.     end
  14.  
I forgot to serialize the form and put data on my ajax sorry for the very simple error, , before without serializing the the form my inputs are being save on my database so i thought there is no problem if i did not serialize the form or put data on my ajax code, , so finally i made this code. . . :) thank you for your response @RABBIT :)

3 6978
Exequiel
288 256MB
I made the solution already. . . .
May 28 '14 #2
Rabbit
12,516 Expert Mod 8TB
Can you post your solution in case someone else runs into the same problem?
May 28 '14 #3
Exequiel
288 256MB
here are my changes in ajax.
Expand|Select|Wrap|Line Numbers
  1. $('#save').click(function(event){
  2.         event.preventDefault();
  3.         var data = $("form#new_personnel").serialize();
  4.  
  5.             $.ajax({
  6.                 url: '/personnels.json',
  7.                 type: 'POST',
  8.                 data: data,
  9.                 success: function(a){
  10.                     alert(a.message);
  11.                     console.log(a.message);
  12.                     checkform("form#new_personnel");
  13.  
  14.                     //window.location = '/personnels/' + a.id
  15.                 },
  16.                  error: function(request, status, error) {
  17.               //$(".error_output").html(request.responseText);
  18.                 errors = request.responseJSON;
  19.                 console.log(errors.lastname);
  20.  
  21.                 if(typeof errors.lastname !== 'undefined') {
  22.                     for(var error_message in errors.lastname) {
  23.                         $('#personnel_lastname_errors').append(error_message);    
  24.                     }    
  25.                 }
  26.             }
  27.             });
  28.  
  29.     });
  30.  
and here is my controller function create.
Expand|Select|Wrap|Line Numbers
  1. def create
  2.         @personnel = Personnel.new(personnel_params)
  3.  
  4.         respond_to do |format|
  5.             if @personnel.save
  6.                 format.html { render action: 'new' }
  7.                 format.json { render :json => { :status => 'Ok', :message => 'Personnel Successfully Added'}, :status => :created }
  8.             else
  9.                 format.html { render action: 'new' }
  10.                 format.json { render json: @personnel.errors, status: :unprocessable_entity }
  11.             end
  12.         end
  13.     end
  14.  
I forgot to serialize the form and put data on my ajax sorry for the very simple error, , before without serializing the the form my inputs are being save on my database so i thought there is no problem if i did not serialize the form or put data on my ajax code, , so finally i made this code. . . :) thank you for your response @RABBIT :)
May 29 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Jeremy | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.