gpt4 book ai didi

ruby-on-rails - 对来自 2 个不同 Controller 的同一模型执行不同的现场验证

转载 作者:数据小太阳 更新时间:2023-10-29 07:56:45 25 4
gpt4 key购买 nike

我正在尝试生成一个快速注册表格以在我的应用程序上使用 Facebook、Twitter 和 Linked 登录。但是我确实有用户模型可以验证先前注册过程中的某些字段。

我正在尝试基于 Devise 扩展注册 Controller ,以便使用相同的数据库从外部登录进行快速注册。制定这个问题是因为我正在运行试用版,然后我有注册表单询问信用卡。

我有这个 Controller :

class Users::QuickRegistrationController < Users::RegistrationsController

self.model_class = Users::RegistrationsController

end

然后我有这个用户模型:

class User < ActiveRecord::Base  
validates :name, :presence => true
validates :first_surname, :presence => true
validates :prefered_language, :presence => true
validates :dni, :presence => true, :uniqueness => true #, :format => {:with => /(^\d{7,8}[a-zA-Z]$)|(^[a-zA-Z]\d{7}[a-zA-Z]$)/, :unless => "dni.blank?"}
validates :phone_number, :presence => true, :format => { :with => /^((\d{9})|([+]\d{11}))$/, :unless => "phone_number.blank?"}, :uniqueness => true
validates :birthdate, :presence => true, :age => {:adult => :over}
validates :legal_conditions, :acceptance => {:accept => true}
validates :email, :confirmation => true
validates :email_confirmation, :presence => true
validates :password_confirmation, :presence => true, :if => "new_record?"
validates :country, :presence => true

我可以说类似验证 :first_surname, :presence => false 的 Users::QuickRegistrationController 和 :first_surname, :presence => true 的 Users::RegistrationsController 吗?

另外,在Ruby上有解决这个问题的方法,你能提供任何教程或相关链接吗?

最佳答案

正如 23tux 所说,验证是基于模型的。我要做的是向您的模型添加一个新列:

add_column :products, :quick_registration, :boolean, default => true

然后在你的用户模型中:

class User < ActiveRecord::Base  
validates :name, :presence => true
validates :email, :confirmation => true
validates :email_confirmation, :presence => true


with_options :unless => :quick_registration? do |u|

# These only validate during full registration
u.validates :first_surname, :presence => true
u.validates :prefered_language, :presence => true
u.validates :dni, :presence => true, :uniqueness => true #, :format => {:with => /(^\d{7,8}[a-zA-Z]$)|(^[a-zA-Z]\d{7}[a-zA-Z]$)/, :unless => "dni.blank?"}
u.validates :phone_number, :presence => true, :format => { :with => /^((\d{9})|([+]\d{11}))$/, :unless => "phone_number.blank?"}, :uniqueness => true
u.validates :birthdate, :presence => true, :age => {:adult => :over}
u.validates :legal_conditions, :acceptance => {:accept => true}
u.validates :password_confirmation, :presence => true, :if => "new_record?"
u.validates :country, :presence => true

end
end

然后在您的 Controller 中,您可以通过将 quick_registration 设置为 false 来“打开”额外的验证:

@user.quick_registration = false
@user.save # all validations should fire

关于ruby-on-rails - 对来自 2 个不同 Controller 的同一模型执行不同的现场验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13039274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com