0

Web ショップで新しい顧客アカウントをアクティブ化するためのカスタム コントローラー アクションがあります。というモデルがありcustomer.rbます。モデルはキーを生成し、顧客にアクティベーション リンクを送信します。リンクはcustomers_controller.rbおよび次のアクションにルーティングされます。

def activate_customer
  @customer = Customer.find(params[:customer_id])
    if @customer.present?
     if @customer.activation_key == params[:activation_key]
       @customer.activated = true
       @customer.save
       redirect_to :cart
     else
       redirect_to :root
     end
    else
     redirect_to :root
    end
end

最初はなしで試しました@customer.saveが、記録は更新されませんでした。前後にレイズしましたが@customer.activated = true、すべて問題ないようです。唯一の問題は、レコードが更新されないことです。考えられる問題もチェックしましたが、そうではありません(チェックするために完全attar_accessibleに閉じました)。attr_accessible何か案は?事前にサンクス!

*編集: のチェック値@customer.save、戻り値false...

*編集:

問題は Customer モデルでのパスワード検証にあるようです。検証は次のようになります。

# Validations
validates_presence_of :title, :country, :zipcode, :city, :street, :number, :first name, :lastname
validates :email, :presence => true, :uniqueness => true, :email => true
validates_confirmation_of :password, :unless => :validate?
validates_presence_of :password

この特定のコントローラー アクションの検証をスキップする方法はありますか? 一緒に考えてくれてありがとう!

4

1 に答える 1

0

この場合のベスト プラクティスである条件付き検証を使用できます。

これはそのhttp://railscasts.com/episodes/41-conditional-validationsに関するレールキャストです

次に、次のようなコードを使用できます。

attr_accessor :updating_password

validates_confirmation_of :password, :if => should_validate_password?

def should_validate_password?
  updating_password
end

検証をスキップする場合は、updateing_password を false に設定する必要があります。

@customer.updating_password = false
@customer.save
于 2012-07-12T10:33:08.543 に答える