精于设计,验证问题

时间:2019-04-05 14:51:00

标签: ruby-on-rails devise wicked-gem

我正在使用邪恶的宝石来逐步建立候选人资料(向导),并使用 Devise 进行授权。

首先是候选人,需要以最少的信息(名字,姓氏,电子邮件,密码)注册。 (这是设计形式) 因此,候选类需要验证这些字段。此后,应聘者需要注册并需要建立自己的个人资料。 (在本例中为教育的第一步)

因此候选人开始了第一步教育。 Current_step设置为nil,我正在验证教育步骤的字段。 像这样:

cattr_accessor :form_steps do
    %w(education job_preferences employment_preferences work_experiences final_facts)
end

attr_accessor :current_step

validates :email, :first_name, :last_name, presence: true, if: -> { current_step? }
validates :academic_degree_name, presence: true, if: -> { current_step?(:education) }

def current_step?(step = nil)
   current_step.blank? || current_step == step
end

在配置文件构建控制器中:

class ProfileBuildController < ApplicationController
    include Wicked::Wizard

    before_action :set_candidate, only: [:show, :update]

    steps *Candidate.form_steps

    def show
      render_step(params[:id])
    end

    def update
      @candidate.update(candidate_params(step))
      render_wizard @candidate
    end

    private

    def redirect_to_finish_wizard(options = nil)
      redirect_to root_url, notice: "Thank you for signing up."
    end

    def set_candidate
      @candidate = current_candidate
    end

    def candidate_params(step)
      permitted_attributes = case step
                             when "education"
                               [:graduation_date, :academic_degree_name, :qualification_other, :educational_institution]
                             when "job_preferences"
                               [:job_preferences, :available_start_date]
                             when "employment_preferences"
                               [:employment_preferences]
                             when "work_experiences"
                               [:employment_preferences]
                             when "final_facts"
                               [:employment_preferences]
                             end
      params.require(:candidate).permit(permitted_attributes).merge(current_step: step)
    end

end

问题是我不知道如何编写这些验证以在current_step为nil时首先工作,然后再使用步骤。

0 个答案:

没有答案