【问题标题】:How prevent double entry in tab after refresh by the client客户端刷新后如何防止选项卡中的双重输入
【发布时间】:2019-11-18 04:13:06
【问题描述】:

我为我的节日公众建立了一个预订表格。但是我的数组中有很多双重输入。由刷新引起。

提交后如何在 Ruby on Rails 上清理表单?或者通过其他方式防止这个问题?

MAJ:我在这里发布我的视图、我的控制器和我的模型

<%= simple_form_for @booking do |f| %>
    <%= f.error_notification %>

    <%= f.input :first_name, label: 'Prénom' %>
    <%= f.input :last_name, label: 'Nom'  %>
    <%= f.input :email, label: 'Email'  %>
    <%= f.input :phone, as: :integer, label: 'Téléphone'  %>

    <%= f.input :member, as: :integer, label: false, input_html: {id: 'quantite_1', value: 0, min: "0", onchange: 'prix()'} %>
    <%= f.input :member_not, as: :integer, label: false, input_html: {id: 'quantite_2', value: 0, min: "0", onchange: 'prix()'} %>

    <%= f.button :submit, 'Réserver', class: 'uk-button red-full button' %>
<%end%>
class BookingsController < ApplicationController
  before_action :set_booking, only: [:show]

  def index
    @booking = Booking.new
  end

  def edit
  end

  def show
  end

  def create
    @booking = Booking.new(booking_params)

    if @booking.save
      BookingMailer.confirmation_booking(@booking).deliver_now
      BookingMailer.alert_booking(@booking).deliver_now
      redirect_to festivals_path
    else
      render :index, notice: ''
    end
  end

  private

  def set_booking
    @booking = Booking.find(params[:id])
  end

  def booking_params
    params.require(:booking).permit(:email, :first_name, :last_name, :phone, :member, :member_not, :young, :so_young, :exonere, :representation_id)
  end
end
class Booking < ApplicationRecord
  belongs_to :representation

  validate :test

  validates :last_name, presence: true
  validates :first_name, presence: true
  validates :email, presence: true

  private

  def test
    if member_not + member <= 0
      errors.add(:member, "Il vous faut au moins 1 place")
    end
  end
end

有时我会收到两个类似的预订

【问题讨论】:

  • 能否提供更多信息,例如代码 sn-ps?
  • 感谢您的回复:我更新了我的帖子
  • 我认为您的问题仍然不清楚。预订的 simple_form 指向预订控制器,但您只显示了 Admin::ReservationsController。另外,“提交后清理表单”是什么意思?
  • 我尽量说得更清楚 :) 当用户在我的后台提交预订时,我可能有 2 个类似的条目。当我在提交后回来时就到了:表单再次提交。所以我寻找一种方法来防止这个缺点,我认为清理表单的缓存可能是一个解决方案。你怎么看 ?更清楚了吗?我希望 ;)
  • 您应该为预订添加验证,因此用户不能预订两次(例如预订的 user_id 的唯一性)。但是由于我看不到 bookngs 控制器,所以我不知道为什么您会两次获得相同的预订。一般不会发生

标签: ruby-on-rails forms validation


【解决方案1】:

好的,由于您的控制器或视图中没有可能导致重复输入的代码,因此最简单的方法是对预订模型进行验证。 所以在预订模型中我会添加:

validates :email, uniqueness: true

缺点是,用户只能使用此电子邮件地址预订一次门票。但如果你能忍受它,那应该没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    相关资源
    最近更新 更多