0

質問の意味がわからないので、例を挙げて説明します。

基本的に、アプリには会社のモデルと会社の従業員がいます。従業員はデバイス モデルであり、サインアップ/サインインできます。従業員がサインアップ後に勤務先の会社を選択できるようにウィザードを設定しているため、モデルは会社のネストされた属性を受け入れます。

従業員が勤務先の会社を選択する段階で、従業員のメール ドメインとデータベース内の会社のメール ドメインを照合して、勤務先の会社のみを選択するように検証を設定したいと考えています。どの時点でこれを行う必要がありますか? カスタムバリデータをセットアップするか、コールバックを使用する必要がありますか?

これが私のコードです:

従業員:

class Employee < ActiveRecord::Base

  ##################
  # Base
  ###################

  rolify
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :company_id, :company_attributes


  ##################
  # Associations
  ###################

  belongs_to :company
  accepts_nested_attributes_for :company
  has_many :authentications, dependent: :destroy

end

会社:

class Company < ActiveRecord::Base


  ##################
  # Base
  ###################

  attr_accessible :name, :address_attributes, :email, :phone_number, :website, :confirmed



  ##################
  # Associations
  ###################

  has_one :address
  accepts_nested_attributes_for :address
  has_many :employees


end

そして、これは従業員が会社を選択する責任を負うコントローラーです。これは邪悪な宝石ウィザード コントローラーです。

class EmployeeStepsController < ApplicationController
  before_filter :authenticate_employee!
  include Wicked::Wizard
  steps :personal, :company_details, :enter_company_details

  def show
    @employee = current_employee
    case step
    when :enter_company_details
      if @employee.company
        skip_step
      else
        @employee.build_company.build_address
      end
    end
    render_wizard
  end

  def update
    @employee = current_employee
    @employee.attributes = params[:employee]
    render_wizard @employee
  end

  private

  def finish_wizard_path
    employee_url(@employee)
  end

end

サイト管理者のためにサイトに会社を個別に追加することを処理する別のコントローラーがありますが、従業員が会社を選択したときにウィザードコントローラーで電子メール検証をトリガーしたいだけです。これに関するアドバイスはありますか?

4

1 に答える 1

0
class Employee < ActiveRecord::Base

  ##################
  # Base
  ###################

  rolify
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :company_id, :company_attributes


  ##################
  # Associations
  ###################

  belongs_to :company
  accepts_nested_attributes_for :company
  has_many :authentications, dependent: :destroy

  # HERE
  validate :my_custom_vaildator

  private
    def my_custom_vaildator
      # do stuff .....
      # based off that stuff add errors
      if some_logic_about_your_company?
          self.errors.add(:base, "select the company you work for.")
      elsif some_other_logic?
          self.errors.add(:name, "your name sucks.")
      end
    end  
end

覚えて:

nil または false を返すことはありません。やればBANG!

=)

これはすべて、許可されていないものをフロントエンドが選択することを許可してはならないということです。

于 2013-04-09T10:39:28.893 に答える