【发布时间】:2014-06-24 22:25:48
【问题描述】:
在我的某些表单上,我有一个额外的弹出确认“你确定吗?”在它真正破坏记录之前。我正在使用 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 以编辑促销时,两次而不是仅一次?
在这种情况下,我看了三遍:
【问题讨论】:
-
你能贴出那张快照吗?
-
你能发布你的布局,路线文件吗?
-
删除是破坏性的。声称这是防止意外数据丢失并将其发送的理想行为;] /jokes
标签: javascript forms ruby-on-rails-4 controller destroy