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 -
<%= form_for(@personnel, remote: true) do |e| -%>
-
<% if @personnel.errors.any? %>
-
<div class="alert alert-error">
-
<button type="button" class="close" data-dismiss="alert">×</button>
-
<h2>Errors</h2>
-
<ul>
-
<% @personnel.errors.full_messages.each do |message| %>
-
<li><%= message %></li>
-
<% end %>
-
</ul>
-
</div>
-
<% end %>
-
<fieldset>
-
<div class="form-group">
-
<label for="lastname">Last Name:</label><%= e.text_field :lastname -%>
-
</div>
-
<div class="form-group">
-
<label for="firstname">First Name:</label><%= e.text_field :firstname -%>
-
</div>
-
<div class="form-group">
-
<label for="middlename">Middle Name:</label> <%= e.text_field :middlename -%>
-
</div>
-
<div class="form-group">
-
<label for="extensionname">Extension Name:</label> <%= e.text_field :extensionname -%>
-
</div>
-
<%= e.submit 'Save', :class => 'btn btn-default', :id => 'save' -%>
-
</fieldset>
-
<% end -%>
-
heres my jquery ajax application.js - $(document).ready(function(){
-
-
$('#save').click(function(event){
-
event.preventDefault();
-
-
$.ajax({
-
url: '/personnels.json',
-
type: 'POST',
-
success: function(a){
-
alert(a.message);
-
console.log(a.message);
-
}
-
});
-
-
});
-
-
-
});
-
and here is my defined function create in controller personnels_cont roller.erb -
def create
-
@personnel = Personnel.new(personnel_params)
-
-
respond_to do |format|
-
if @personnel.save
-
format.html { redirect_to @personnel }
-
format.json { render json: { :status => 'Ok', :message => 'Personnel Successfully Added'}, status: :created }
-
else
-
format.html { render action: 'new' }
-
format.json { render json: @personnel.errors, status: :unprocessable_entity }
-
end
-
end
-
end
-
-
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. . :)
3 8629
I made the solution already. . . .
Rabbit 12,516
Recognized Expert Moderator MVP
Can you post your solution in case someone else runs into the same problem?
here are my changes in ajax. -
$('#save').click(function(event){
-
event.preventDefault();
-
var data = $("form#new_personnel").serialize();
-
-
$.ajax({
-
url: '/personnels.json',
-
type: 'POST',
-
data: data,
-
success: function(a){
-
alert(a.message);
-
console.log(a.message);
-
checkform("form#new_personnel");
-
-
//window.location = '/personnels/' + a.id
-
},
-
error: function(request, status, error) {
-
//$(".error_output").html(request.responseText);
-
errors = request.responseJSON;
-
console.log(errors.lastname);
-
-
if(typeof errors.lastname !== 'undefined') {
-
for(var error_message in errors.lastname) {
-
$('#personnel_lastname_errors').append(error_message);
-
}
-
}
-
}
-
});
-
-
});
-
and here is my controller function create. -
def create
-
@personnel = Personnel.new(personnel_params)
-
-
respond_to do |format|
-
if @personnel.save
-
format.html { render action: 'new' }
-
format.json { render :json => { :status => 'Ok', :message => 'Personnel Successfully Added'}, :status => :created }
-
else
-
format.html { render action: 'new' }
-
format.json { render json: @personnel.errors, status: :unprocessable_entity }
-
end
-
end
-
end
-
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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
...
|
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.
|
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...
|
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...
| |
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
|
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){...
|
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?
...
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |