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.")
上述警報我會顯示當前的車輛名稱刪除。但是,如果我更新使用內聯編輯後,我想刪除我會顯示以前的記錄。
例子:
車輛名稱:MH P5 2312,我想刪除它,該警報顯示是刪除 「MH P5 2312」 車輛。
在線編輯後,我將更改車輛名稱:AP 16 1234,所以我想刪除車輛名稱:AP 16 1234,但ajax警報顯示,車輛名稱:MH P5 2312刪除車輛名稱。
高級謝謝。