Thursday 2 March 2017

Ruby On Rails Form with Ajax by remote => true

<<<==============================Ruby Ajax Form Submitting Syntax =======================>>> 
 
=form_for @modelObject, :remote => true,:html => {:'data-type' => 'json',:onSubmit=>"checkform()"}
,:url => url_path([@paramsinUrl]) do |f|
 
 
 
 
<<<<=======================================Controller==============================>
 
def create
 @modelObject =  ModelObjectClass.new(params['params_name']) 
 respond_to do |format|
   if @modelObject.save
       format.html { redirect_to :back }
       format.json { render :json => {:status=>"success",:message=>'Added Sucessfully.'} } 
   else
     format.html { redirect_to :back }
     format.json { render :json => {:status=>"error",:message=>'Error Message.'} } 
   end 
 end 
 
end 
 
 
 
<<<<==================================Javascript======================================>
 
$("#form_id").live("ajax:beforeSend", function(e, xhr, settings) {
  

});
$("#new_manual_air_booking").live("ajax:success", function(e, data, status, xhr) {
   alert("Added");
   render_time_slot(DI.day_wrapper_id,DI.day_date,DI.trip_id);
   $("#daily_itinerary_event_model").html('');

}).live("ajax:error", function(e, xhr, status, error) {
    alert("Error");
});  

 


=======================================OR================================================ 
 live => on

Instead of live you can use on because live deprecated few year back, syntax for using on is:
 
$(document).on('ajax:beforeSend','#form_id',function(e, xhr, settings) {
  

}); 
 
 Instead of document you can wright parent element id of element which we are going to target.
 

No comments:

Post a Comment