1

我正在尝试提供一个位置来为帐户设置单个服务登录,但不要求帐户所有者在每次更新记录的其余部分时都输入服务登录凭据。

我的理解是,:reject_if选项 onaccepts_nested_attributes_for是忽略嵌套哈希值的方式。然而,在 Rails 4.1 中,我得到一个“密码不能为空”。

我已经跟踪了nested_attributes 代码,它似乎正确地忽略了这些值,但我没有做任何事情来避免更新工作。我什web_service_user_attributes至从传递给的参数中删除了哈希update,所以我想知道是否还有其他事情发生。

我对协会的理解:reject_if是否正确has_one

父型号代码:

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
4

1 回答 1

0

好的,看来我的主要错误是试图验证: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
于 2014-05-27T15:42:20.913 回答