邪恶的向导未在第一步中设置current_step

时间:2019-04-06 09:42:57

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

我正在使用 Wicked 宝石来逐步(向导)构建候选个人资料,并使用 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

结束

重定向到第一步后,current_step为nil,因此我的验证无法正常工作。 帮助吗?

0 个答案:

没有答案