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。如果你这样做,它会爆炸!

=)

所有这一切都表明前端永远不应该允许他们选择不允许的东西。

于 2013-04-09T10:39:28.893 回答