473,233 Members | 1,559 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

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 8426
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

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

Similar topics

1
nirmalsingh
by: nirmalsingh | last post by:
i have an html form ,web form and javascript file.(using ajax technology), html page is start up page , server side codings are written in web form. now the problem is i want to display message in...
1
by: RubyNovice | last post by:
User Community, I just registered on TheScripts.com and I'm looking for help building an intranet for my employer using Ruby on Rails running on RedHat Linux. I'm new to Ruby; I just purchased my...
5
by: Heofz | last post by:
Hey all, I've been banging my head against a brick wall on this one for the last 12 hours, and the time has come for me to give up and consult the gurus on this one. The below URL contains a...
3
by: Jeremy | last post by:
I've got a button in an update panel. In the click event of the button (server side code), I perform some logic and under certain criteria I want to display an alert to the user. I've tried...
1
by: cmrhema | last post by:
Hi, I am working in asp.net+ajax. I do not know where to put this question. I guess i am in the right place I am using ajax update control panel. I have three dropdownlist inside it. Now the...
1
by: idealfellow | last post by:
Hi All, I need to delete entire column(GB) in excel using ruby code. let me whether i should write some ruby code or is there a way to call an excel macro from ruby whichever is simpler...
3
by: neovantage | last post by:
Hi Experts, I have create a jQuery ui function which delete product from the shopping list. After receiving a jQuery Ajax response i want to bind the same function which load on page load. How can i...
0
by: Manuel Scholten | last post by:
Hallo, I seem to have a sneaky little problem in my ajax-based function. The function is supposed to check at the server if the given username/password-combination is valid. On server-side...
18
by: hannoudw | last post by:
hi i'm trying to insert an Arabic word into the database in Ajax using the POST method . for example i want to insert "ت" but i'm getting "%u062A" instead ... does any one can help me ? thanks in...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.