0
我有一个窗体,用户可以订阅最新优惠券。当我打开同一商店链接在多个选项卡和订阅,我得到多个订阅单个商店导致数据重复并发送相同邮件多次。所以添加验证模型。所以问题是我如何得到错误显示在视图中。当用户已订阅store.Since创建一个ajax请求..我已经粘贴的代码下方。任何帮助,将不胜感激谢谢显示模型基础错误在创建rails 3视图3
_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 %>