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
heres my jquery ajax application.js
and here is my defined function create in controller personnels_cont roller.erb
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. . :)
heres the _form.html.erb
Code:
<%= 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 -%>
Code:
$(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);
}
});
});
});
Code:
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
Comment