gpt4 book ai didi

javascript - 销毁记录前的额外 "Are you sure?"

转载 作者:行者123 更新时间:2023-11-30 10:14:56 32 4
gpt4 key购买 nike

在我的一些表格上,我有一个额外的弹出确认“你确定吗?”在它真正破坏记录之前。我正在使用 Rails 4 和 simple_form。这是一个例子。

我有一个名为 Promotions 的模型和另一个名为 PromotionPurchase 的模型。

模型:

class Promotion < ActiveRecord::Base
has_many :promotion_purchases, dependent: :destroy
end

class PromotionPurchase < ActiveRecord::Base
belongs_to :user
belongs_to :promotion
end

架构

create_table "promotion_purchases", force: true do |t|
t.integer "user_id"
t.integer "promotion_id"
t.string "status"
t.string "stripe_card_token"
t.string "phone"
t.string "full_name"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "promotion_purchases", ["promotion_id"], name: "index_promotion_purchases_on_promotion_id"
add_index "promotion_purchases", ["user_id"], name: "index_promotion_purchases_on_user_id"

create_table "promotions", force: true do |t|
t.string "name"
t.text "description"
t.integer "cost_in_cents"
t.integer "amount_available"
t.string "category"
t.datetime "start_date"
t.datetime "end_date"
t.datetime "created_at"
t.datetime "updated_at"
end

和形式:

<div class="content-box">
<%= simple_form_for [:admin, @promotion] do |f| %>
<fieldset id="promotion-form-info">
<%= f.input :name %>
<%= f.input :description %>
<%= f.input :category %>
<%= f.input :cost_in_cents, as: :string %>
<%= f.input :amount_available, as: :string %>
<%= f.input :start_date, :as => :date_picker %>
<%= f.input :end_date, :as => :datetime_picker %>
<%= f.button :submit, class: 'btn btn-sm btn-success' %>
</fieldset>
<% end %>
<% if @promotion.id != nil %>
<fieldset id="promotion-form-purchases">
<h3>Purchases:</h3>
<% if !@purchases.empty? %>
<table class="table">
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Status</th>
<th></th>
</tr>
<% @purchases.each do |purchase| %>
<tr>
<td><%= purchase.full_name %></td>
<td><%= purchase.user.email if purchase.user %></td>
<td><%= purchase.phone %></td>
<td><%= purchase.status %></td>
<td>
<%= link_to "Edit", edit_admin_promotion_promotion_purchase_path(@promotion, purchase), class: "btn btn-xs btn-success" %>
<%= link_to 'Delete', [:admin, @promotion, purchase], method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-xs btn-danger" %>
<%= "<- Editing this purchase" if @purchase && purchase.id == @purchase.id %>
</td>
</tr>
<% end %>
</table>
<% end %>
<p>
<%= link_to "Add Purchase", new_admin_promotion_promotion_purchase_path(@promotion), class: "btn btn-sm btn-primary" %>
</p>
</fieldset>
<% end %>
</div>

和 Controller

class Admin::PromotionPurchasesController < ApplicationController
layout 'admin'
before_action :set_promotion
before_action :set_purchase, only: [:edit, :update, :destroy]

def destroy
@purchase.destroy
redirect_to new_admin_promotion_promotion_purchase_path(@promotion), success: 'PromotionPurchase was deleted.'
end

private

def set_promotion
@promotion = Promotion.find(params[:promotion_id])
end

def set_purchase
@purchase = PromotionPurchase.find(params[:id])
end
end

知道为什么我会看到“你确定吗?”当我从表单中销毁 PromotionPurchase 以编辑 Promotion 时两次而不是一次?

这种情况,我看了三遍:

A list of purchases connected to this Promotion The first "Are you sure? The second The third

最佳答案

您发布的代码似乎没问题。

我认为问题出在 javascript 上。 (检查控制台你可能会在那里看到一些东西)

您可能还多次导入了jquery_ujs,尝试从application.js 中删除jquery_ujs 并查看是否发生任何事情,如果是只导入一次,你不应该得到那个对话框。

关于javascript - 销毁记录前的额外 "Are you sure?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24397364/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com