2

Vehicles_controller.rb

     class VehiclesController < ApplicationController
  before_action :set_vehicle, only: [:show, :edit, :update, :destroy]
  load_and_authorize_resource

   # skip_before_action :verify_authenticity_token

  # GET /vehicles
  # GET /vehicles.json
  def index
    @q = Vehicle.search(params[:q])
    @vehicles = @q.result(:distinct => true).order_by([:updated_at, :desc]).page(params[:page]).per(5)
    @vehicle = Vehicle.new
  end

  # GET /vehicles/1
  # GET /vehicles/1.json
  def show
  end

  # GET /vehicles/new
  def new

  end

  # GET /vehicles/1/edit
  def edit
  end

  # POST /vehicles
  # POST /vehicles.json
  def create
    params[:vehicle][:name] = params[:vehicle][:name].upcase if !params[:vehicle][:name].nil?
    @vehicle = Vehicle.new(vehicle_params)
    @vehicles = Vehicle.all.order_by([:updated_at, :desc]).page(params[:page]).per(5)
    respond_to do |format|
      if @vehicle.save
        format.html { redirect_to :back, notice: 'Vehicle was successfully created.' }
        format.json { render action: 'index', status: :created, location: @vehicle }
        format.js
      else
        format.js
        format.html { render action: 'new' }
        format.json { render json: @vehicle.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /vehicles/1
  # PATCH/PUT /vehicles/1.json
  def update
        params[:vehicle][:name] = params[:vehicle][:name].upcase if !params[:vehicle][:name].nil?

    respond_to do |format|

      if @vehicle.update(vehicle_params)
        format.html { redirect_to @vehicle, notice: 'Vehicle was successfully updated.' }
        format.json {render json: @vehicle, status: :ok}
      else
        format.html { render action: 'edit' }
        format.json { render json: @vehicle.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /vehicles/1
  # DELETE /vehicles/1.json
  def destroy
    @vehicle.destroy
    respond_to do |format|
      format.html { redirect_to vehicles_url, notice: "#{@vehicle.name} deleted successfully" }
      format.json { head :no_content }
      format.js { render :layout => false}
    end
  end

  def vehicle_search

    @q = Vehicle.search(params[:q])
    @vehicles = @q.result(:distinct  => true).order_by([:updated_at, :desc]).page(params[:page]).per(5)

    respond_to do |format|
      format.html
      format.js
    end
  end
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_vehicle
      @vehicle = Vehicle.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def vehicle_params
      params.require(:vehicle).permit(:name, :created_at, :updated_at)
    end
end

index.html.erb

 <% @vehicles.each do |vehicle| %>
<tr>
    <td><%= best_in_place vehicle,:name, class: "v_name", id: vehicle.id%></td>
    <td><%= link_to '<i class="fa fa-trash-o"></i>'.html_safe, vehicle, method: :delete,  remote: true, data: { confirm: "Are you sure to delete <b>\"#{vehicle.name}\"?</b>", commit: "OK" }, title: "Delete Vehicle", class: "btn btn-danger delete_vehicle" %>

    </td>
</tr>       
<%end%>

vehicle.rb

    class Vehicle
  include Mongoid::Document
    include Mongoid::Timestamps

  field :name, type: String
  validates :name, presence: true, uniqueness: true, :format => {:with => /[1-9a-zA-Z][0-9a-zA-Z ]{3,}/}
  has_many :pre_processings
  has_many :batch_counts


end

destroy.js.erb

 $('.delete_vehicle').bind('ajax:success', function() {  
        $(this).closest('tr').fadeOut();
});
$(".alert").show();
$(".alert").html("Vehicle \"<b><%[email protected] %></b>\" deleted successfully.")
$(".alert").fadeOut(5000);

Here i am using destroy.js.erb to delete vehicle name. It works fine.

Here i am using Inline Edit for Best_in_place. After update the Vehicle name, The ajax alert shows previous vehicle name. Not update vehicle name. So how to show updated vehicle name alert.

$(".alert").html("Vehicle \"<b><%[email protected] %></b>\" deleted successfully.")

The above alert i will show current vehicle name to delete. But if i update using inline edit after i want to delete i'll shows previous record.

Example:

  1. Vehicle Name: MH P5 2312 , I want to delete it, The alert shows are you delete "MH P5 2312 " Vehicle.

  2. After inline edit i will change Vehicle Name: AP 16 1234, So i want to delete Vehicle Name: AP 16 1234, but the ajax alert shows, Vehicle Name: MH P5 2312 delete vehicle name.

Advance Thanks.

2 Answers 2

1

This is because you need to update the alert message on ajax request success

in your destroy.js.erb

you should ditch the .erb and fetch the car name from an html element instead

your js code should be as the following:

$('.delete_vehicle').bind('ajax:success', function() { 
    var vehicleName = $('vehicle_link').data('vehicle_name');
    // here I assume that you have a link for initiating the delete process and you can add an attribute to this anchor tag element called data-vehicle_name and set its value to the car name.
    $(this).closest('tr').fadeOut();
    $(".alert").show();
    $(".alert").html("Vehicle \"<b>" + vehicleName + "</b>\" deleted successfully.");
    $(".alert").fadeOut(5000);
});
0

Firstly, if you're going to use the ajax:success callback (which you don't in this instance), you'll need to bind the alert functions to it:

$('.delete_vehicle').on('ajax:success', function() {  
    $(this).closest('tr').fadeOut();
    $(".alert").show().html("Vehicle \"<b><%[email protected] %></b>\" deleted successfully.").fadeOut(5000);
});

Secondly, if you're not getting an updated @vehicle.name because - I think - your Rails instance will have already rendered it. I'm not exactly sure why, but the key will be to pass your data through the ajax request if you were still going to use it:

#app/controllers/vehicles_controller.rb
class VehiclesController < ApplicationController
   def destroy
        ...
        format.js { render :layout => false, json: @vehicle } 
   end
end 

#app/assets/javascripts/application.js
$('.delete_vehicle').on('ajax:success', function(event, data, status, xhr) {  
    vehicle = JSON.parse(data);
    $(this).closest('tr').fadeOut();
    $(".alert").show().html("Vehicle \"<b>" + + "</b>" deleted successfully.").fadeOut(5000);
});

--

Bottom line is that js.erb is meant to give you actionable Javascript functionality from the controller action. I think your main problem is you're using ajax within this, likely causing a conflict.

Above is how I'd use Ajax (IE put the code in the javascript assets folder); if you wanted to use destroy.js.erb still, you could use the following:

#app/views/vehicles/destroy.js.erb
$("<%= @vehicle.id %>").closest('tr').fadeOut();
$(".alert").show();
$(".alert").html("Vehicle \"<b><%[email protected] %></b>\" deleted successfully.")
$(".alert").fadeOut(5000);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.