Ruby on rails 有一个,如果拒绝,接受嵌套属性,进行静态触发器验证
我试图提供一个为帐户设置单一服务登录的位置,但不要求帐户所有者在每次更新其余记录时输入服务登录凭据 我的理解是,Ruby on rails 有一个,如果拒绝,接受嵌套属性,进行静态触发器验证,ruby-on-rails,devise,ruby-on-rails-4.1,Ruby On Rails,Devise,Ruby On Rails 4.1,我试图提供一个为帐户设置单一服务登录的位置,但不要求帐户所有者在每次更新其余记录时输入服务登录凭据 我的理解是,上的:reject\u if选项接受的嵌套属性是忽略嵌套哈希值的方法。然而,在Rails4.1中,我得到了一个“密码不能为空” 我已经跟踪了嵌套的_属性代码,它似乎正确地忽略了这些值,但是我没有做任何事情来避免更新。我甚至从传递给update的参数中删除了web\u service\u user\u属性散列,因此我想知道是否还有其他问题 我是否理解了:如果与某个关联正确,则拒绝 父模型
上的:reject\u if
选项接受
的嵌套属性是忽略嵌套哈希值的方法。然而,在Rails4.1中,我得到了一个“密码不能为空”
我已经跟踪了嵌套的_属性代码,它似乎正确地忽略了这些值,但是我没有做任何事情来避免更新。我甚至从传递给update
的参数中删除了web\u service\u user\u属性
散列,因此我想知道是否还有其他问题
我是否理解了:如果与某个关联正确,则拒绝
父模型代码:
class Account
has_one :web_service_user
accepts_nested_attributes_for :web_service_user, :allow_destroy => true, :reject_if => :password_not_specified, :update_only => true
def password_not_specified(attributes)
attributes[:password].blank?
end
end
class WebServiceUser
devise :database_authenticatable
belongs_to :account
validates_uniqueness_of :username
validates_presence_of :password, if: Proc.new{|wsu| !username.blank? }
end
子模型代码:
class Account
has_one :web_service_user
accepts_nested_attributes_for :web_service_user, :allow_destroy => true, :reject_if => :password_not_specified, :update_only => true
def password_not_specified(attributes)
attributes[:password].blank?
end
end
class WebServiceUser
devise :database_authenticatable
belongs_to :account
validates_uniqueness_of :username
validates_presence_of :password, if: Proc.new{|wsu| !username.blank? }
end
控制器代码:
def update
respond_to do |format|
if @licensee.update(account_params)
#etc...
end
private
def account_params
params.require(:account).permit(:name, :area_of_business, :address1, :address2, :city, :state_code, :zip, :website_url, :web_service_user_attributes => [:id, :username, :password, :_destroy])
end
好的,看来我的主要错误是试图验证是否存在:password
。我真的很想验证密码的长度,如果它存在的话
class WebServiceUser
devise :database_authenticatable
belongs_to :account
validates_uniqueness_of :username
validates_length_of :password, :minimum => 14, if: Proc.new { |u| !u.password.nil? }
end