如何在Rails中创建关联之前设置自定义验证

时间:2013-04-09 10:16:54

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

不确定这个问题是否有意义所以我将通过一个例子来描述:

基本上我的应用程序和公司员工都有公司模型。员工是一个设计模型,可以注册/登录。我为员工设置了一个向导,可以在注册后选择他们工作的公司,因此模型接受公司的嵌套属性。

在他们选择所在公司的阶段,我想设置一个验证,以确保他们只选择员工的电子邮件域与我公司的数据库中的公司电子邮件域,从而选择他们所在的公司。我应该在这一点上做什么?我应该设置自定义验证器还是使用回调?

这是我的代码:

员工:

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

我有另一个控制器处理将网站添加到站点管理员的网站,但我只想在向导控制器中触发电子邮件验证,也就是当员工选择他们的公司时。对此有何建议?

1 个答案:

答案 0 :(得分: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。如果你这样做将会爆炸!

=)

所有这些都说前端不应该允许他们选择不允许的东西。