ユーザーが最新のクーポンをサブスクライブできるフォームがあります。同じストアのリンクを複数のタブで開いてサブスクライブすると、単一のストアで複数のサブスクリプションを取得すると、データが重複し、同じメールが複数回送信されます。モデルの検証が追加されました。質問エラーが表示されるようにするにはどうすればよいですか。ユーザーがすでにストアに登録している場合。createがajaxリクエストを行っているため、以下のコードを貼り付けました。ご協力いただければ幸いです。ありがとうございます。
_index.html.haml(ビュー)
=form_for([@merchant,@coupon_subscription],:remote => true,:html => { :class => "store_subscribe" } ) do |f|
%h3 subscribe for #{@merchant.merchant_name} coupons
-if !logged_in?
=f.text_field :user ,:class => "input_text email"
=f.submit "Subscribe",:disable_with => "Subscribing...",:class => "store_subscribe subscribe_button"
サブスクリプションコントローラー
def create #creates subscription
return if !logged_in?
return if current_user.subscription_limit?
@merchant=Merchant.find_by_permalink(params[:merchant_id])
@coupon_subcription=CouponSubscription.new(:merchant_id => @merchant.id,:user => current_user)
@coupon_subcription.coupon_subscribe
respond_to do |format|
if @coupon_subcription.save!
format.html { redirect_to(:back, :notice => 'Success.') }
format.js
else
format.html { redirect_to(:back, :notice => @coupon_subcription.errors.full_messages || "Oops something went wrong")}
format.js
end
end
UserMailer.delay.coupon_subscription(current_user,@coupon_subcription)
終わり
モデル
validate :validate_subscription ,:on => :create
private
def validate_subscription
@coupon_subscribed=CouponSubscription.find_by_user_id_and_merchant_id_and_active(self.user_id,self.merchant_id,true)
if @coupon_subscribed
self.errors.add(:base , 'You have already subscribed.')
end
終わり
create.js
<% if !logged_in? %>
$(".store_subscribe").remove()
$(".subscription_feedback").show().append("Login!")
<% else %>
$(".store_subscribe").remove()
$(".store_subscribe").show().append("<h3>Thanks for subscription .We have send you a mail </h3>")
<% end %>