Rails:根据失败的activeRecord save指定自定义操作

时间:2012-05-07 19:23:12

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord

class Subscription < ActiveRecord::Base
  validates :feed_id, presence: true, uniqueness: true

现在我确认用户无法按照上述代码订阅相同的Feed两次,我想在通知中指明他们已经订阅了Feed。如果验证失败,我如何指定条件,发送通用“订阅失败”之外的通知

class SubscriptionsController < ApplicationController
def new
#do things
    if @subscription.save
      redirect_to reader_url, notice: "You are now subscribed to: "+feed.title
    else
      redirect_to reader_url, notice: "Subscribe was unsuccessful!"
    end
  end

1 个答案:

答案 0 :(得分:2)

如果验证失败,它将填充@subscription.errors,您可以根据需要检查并显示消息。