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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Exequiel
    Contributor
    • Jul 2012
    • 288

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

    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
    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">&times;</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
    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);
    			}
    		});
    		
    	});
    	
    
    });
    and here is my defined function create in controller personnels_cont roller.erb
    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
    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. . :)
  • Exequiel
    Contributor
    • Jul 2012
    • 288

    #2
    I made the solution already. . . .

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Can you post your solution in case someone else runs into the same problem?

      Comment

      • Exequiel
        Contributor
        • Jul 2012
        • 288

        #4
        here are my changes in ajax.
        Code:
        $('#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.
        Code:
        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 :)

        Comment

        Working...