在我的某些表单上,我有一个额外的弹出确认“你确定吗?”在它真正破坏记录之前。我正在使用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>
和控制器
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以编辑促销时,两次而不是仅一次?
在这种情况下,我看了三次:
答案 0 :(得分:4)
您发布的代码似乎没问题。
我认为问题来自javascript。 (检查控制台,你可能会看到那里的东西)
您也可以多次导入jquery_ujs
,尝试从jquery_ujs
中移除application.js
并查看是否有任何问题,如果只导入一次,则不应该获得该对话框
答案 1 :(得分:0)
尝试在表单中放一个简单的<script>
,并在那里发出警报。如果该警报提醒3次,那么您的问题是加载文件的次数太多了。