Я работаю с многошаговой формой, и поэтому мне нужны частичные проверки. Вот моя модель.
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:omniauthable, omniauth_providers: %i[facebook]
cattr_accessor :form_steps do
%w(address)
end
attr_accessor :current_step
has_one :profile, dependent: :destroy
after_create :create_profile
accepts_nested_attributes_for :profile
validates :first_name, presence: true
validates :last_name, presence: true
validates :street, presence: true, if: -> { current_step?(:address) }
validates :house_number, presence: true, if: -> { current_step?(:address) }
validates :city, presence: true, if: -> { current_step?(:address) }
validates :zip_code, presence: true, if: -> { current_step?(:address) }
def current_step?(step)
current_step == step
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0, 20]
name = auth.info.name
user.first_name = name.split(" ")[0]
user.last_name = name.split(" ")[1]
end
end
end
К сожалению, этот бит не работает, что означает, что проверки шага: адрес не работают, и это позволит пользователю зарегистрироваться без заполнения формы на шаге: адрес.
validates :street, presence: true, if: -> { current_step?(:address) }
validates :house_number, presence: true, if: -> { current_step?(:address) }
validates :city, presence: true, if: -> { current_step?(:address) }
validates :zip_code, presence: true, if: -> { current_step?(:address) }
Почему? Что здесь не так?
Вот UserSteps_Controller
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :address
def show
@user = current_user || User.from_omniauth(request.env["omniauth.auth"])
render_wizard
end
def update
@user = current_user || User.from_omniauth(request.env["omniauth.auth"])
@user.update!(user_params)
render_wizard @user
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :street, :house_number, :city, :zip_code)
end
def redirect_to_finish_wizard(options = nil, params = nil)
redirect_to new_user_profile_path(current_user)
end
end
Спасибо.