如何使用 wicked 进行验证? [英] how validation is done using wicked?
问题描述
我有一个 users_controller 和一个 user_steps_controller,它们有三个步骤:业务、:付款和:登录
I have a users_controller and a user_steps_controller which has three steps :business, :payment and :login
在 user.rb 模型中
In the user.rb model
class User < ActiveRecord::Base
validates_presence_of :fname, :lname, :email, :mob, :country, :state, :suburb, :postal ,:add
end
在检查验证时,如果我输入了一些随机值,它也会给出错误
while checking validation if i put some random values then also it is giving errors
Fname can't be blank
Lname can't be blank
Email can't be blank
Mob can't be blank
Country can't be blank
State can't be blank
Suburb can't be blank
Postal can't be blank
Add can't be blank
请帮帮我
这是我的 users_controller
This is my users_controller
def new
@user = User.new
end
def create
@user = User.new(params[:id])
if @user.save
session[:user_id]= @user.id
@user.update_attributes(user_params )
redirect_to user_steps_path
else
render :new
end
end
private
def user_params
params.require(:user).permit( :fname, :lname, :email, :mob, :gender_male, :gender_female, :country, :state, :suburb, :postal ,:add, :cmpyname, :abnacn, :cmpyadd, :cmpydet,:cash, :paypal,:bsb,:usrname,:password_hash, :password_salt, :selcat, :protit, :prodes)
end
用户.rb
类用户
class User < ActiveRecord::Base
validates :fname, :lname, :email, :mob, :country, :state, :suburb, :postal ,:add, :presence => true
attr_accessor :current_step
validates_presence_of :cmpyname, :abnacn, :cmpyadd, :cmpydet, if: -> { current_step?(:business) }
validates_presence_of :usrname,:password_hash, :password_salt, :selcat, :protit, :prodes, if: -> { current_step?(:login) }
def current_step?(step_key)
current_step == step_key
end
end
user_steps_controller
user_steps_controller
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :business, :login, :payment
def show
@user = current_user
render_wizard
end
def update
@user = current_user
params[:user][:current_step] = step
@user.update_attributes(user_params )
render_wizard @user
end
private
def user_params
params.require(:user).permit( :cmpyname, :abnacn, :cmpyadd, :cmpydet,:cash, :paypal,:bsb,:usrname,:password_hash, :password_salt, :selcat, :protit, :prodes)
end
end
推荐答案
你真正的问题在这里:
@user = User.new(params[:id])
我假设 params[:id]
是 nil
,否则会失败.基本上,您在不提供数据的情况下实例化 User
并尝试保存它.很明显,您提供的那些验证将失败.如果您实际提交包含用户数据的表单,则需要传递您已经定义如下的 user_params
:
I'm assuming params[:id]
is nil
, because otherwise that would fail. Basically, your instantiating a User
with no data supplied and trying to save it. So clearly those validations you have supplied will fail. If you're actually submitting a form with user data you need to pass user_params
you've already defined as follows:
@user = User.new(user_params)
@user = User.new(user_params)
如果您需要在不同步骤中对用户模型进行验证,则需要根据表单的状态有条件地运行验证:
If you need to have validations occur on your User model in different steps, you'll need to have the validations run conditionally dependent on the state of the form:
class User
attr_accessor :current_step
validates_presence_of :business_related_attr, if: -> { current_step?(:business) }
def current_step?(step_key)
current_step.blank? || current_step == step_key
end
end
这篇关于如何使用 wicked 进行验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!