2015-09-22 47 views
2

Vehicles_controller.rb如何在rails中更新Ajax刪除記錄?

 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); 

這裏我使用的是destroy.js.erb來刪除車輛名稱。它工作正常。

在這裏,我使用內聯編輯Best_in_place。更新車輛名稱後,ajax警報將顯示之前的車輛名稱。不更新車輛名稱。那麼如何顯示更新的車輛名稱警報。

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

上述警報我會顯示當前的車輛名稱刪除。但是,如果我更新使用內聯編輯後,我想刪除我會顯示以前的記錄。

例子:

  1. 車輛名稱:MH P5 2312,我想刪除它,該警報顯示是刪除 「MH P5 2312」 車輛。

  2. 在線編輯後,我將更改車輛名稱:AP 16 1234,所以我想刪除車輛名稱:AP 16 1234,但ajax警報顯示,車輛名稱:MH P5 2312刪除車輛名稱。

高級謝謝。

回答

1

這是因爲你需要更新alert messageajax request success

在destroy.js.erb

你應該溝.erb並從html element代替

取車名

您的js code應該如下:

$('.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

首先,如果你要使用ajax:success回調(你在這種情況下沒有),你需要的alert功能綁定到它:

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

其次,如果你沒有得到更新的@vehicle.name,因爲 - 我認爲 - 你的Rails實例已經呈現它。我不知道是什麼原因,但關鍵是通過Ajax請求來傳遞數據如果你仍然要使用它:

#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); 
}); 

-

底線是js.erb旨在爲您提供來自控制器操作的可執行的Javascript功能。我認爲你的主要問題是你在這裏使用ajax,可能會導致衝突。以上是我如何使用Ajax(IE把代碼放在javascript assets文件夾中);如果您仍想使用destroy.js.erb,則可以使用以下內容:

#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);