473,626 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Exequiel
288 Contributor
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_cont roller.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
3 8629
Exequiel
288 Contributor
I made the solution already. . . .
May 28 '14 #2
Rabbit
12,516 Recognized Expert Moderator MVP
Can you post your solution in case someone else runs into the same problem?
May 28 '14 #3
Exequiel
288 Contributor
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
4714
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 html form by calling javascript function through c# ( ie:web form). i have tried string jscript = "<script language='javascript'>hallo();</script>"; Page.RegisterClientScriptBlock("keyClientBlock",jscript); but i cant get result. plz help me...
1
6827
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 first technical book yesterday. The book is "Programming Ruby" by Dave Thomas. If you're aware of a site where I may download some sample Ruby on Rails code to get me started I would appreciate it. Thank you for your assistance in the matter. ...
5
6404
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 login page, you can test it using the details User: marco Pass: polo This seems to work PERFECTLY in opera, and it works in firefox but ONLY if I have firebug enabled. I don't care about IE for now, I'll sort that out later.
3
3801
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 several things like adding an onload javascript event to one of the controls in the update panel, and I've tried adding a lteral control to the update panels ContentTemplateContainer.Controls collection where the literal controls contents where...
1
2436
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 user has to choose values from the three dropdownlist. If he chooses the same item as that of previous dropdownlist. I want to display an alert message. I cannot use response.write("<script> alert("dfs")</script>"); I am supposed to write in...
1
4131
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 or easy way to maintain. cheers
3
9586
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 bind the same function after receiving the Response. Here is my JavaScript Code $( ".rData" ).click(function() { $( "#dialog-confirm:ui-dialog" ).dialog( "destroy" ); myLink = $(this).attr("href"); $.urlParam = function(name){...
0
1269
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 everything is working correctly (Output is either "OK" or "Error") but within the js-application I always receive an alert-box stating "undefined" in the very end. Could you please take a look at my code and give me a hint where the problem is? ...
18
6576
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 advance
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8358
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7188
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6119
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1504
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.