- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个用户模型
class User < ActiveRecord::Base
has_many :languages, :dependent => :destroy
accepts_nested_attributes_for :languages, :reject_if => lambda { |l| l[:name].blank? }
end
it "should not save language without name by accepts_nested_attributes" do
lambda {
@user.update_attributes!("languages_attributes"=>{"0"=>{}})
}.should_not change(Language, :count)
end
it "should save language with name by accepts_nested_attributes" do
lambda {
@user.update_attributes!("languages_attributes"=>{"0"=>{"name"=>"lang_name"}})
}.should change(Language, :count).by(1)
end
最佳答案
我看到您希望测试 reject_if
那么最好的方法是直接测试它:
anaf_for_languages = User.nested_attributes_options[:languages]
anaf_for_languages[:reject_if].call({ "name" => "" }).should be_true
true
,然后
name
是空白。我认为这比您的代码更简洁,但并不那么明显。
关于ruby-on-rails - 测试 :reject_if in anaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4433823/
我正在开发一个 ROR 应用程序,但在设计和密码确认方面我遇到了困难。我希望我的用户能够编辑他们的信息(姓名、位置等)而无需输入和确认他们的密码(除非他们决定更改密码,在这种情况下,这些字段将是必需的
我有一个用户模型 class User :destroy accepts_nested_attributes_for :languages, :reject_if => lambda { |
class Gift proc { |a| a['amount'].blank? } 贡献有一个 :nickname 属性。在 :new 表单中,它预先填充了用户的真实姓名。用户可能决定将其更改为“
我放弃尝试覆盖 autosave参数,因为我认为它无法完成。 我搬了has_shipping_address来自 Order到 ShippingAddress模型,现在我有: #the models.
我有一个带有嵌套属性的表单。现在在我的 :reject_if =>声明我想检查嵌套模型上的属性,比如 first_record?有没有办法访问这样的方法?在我看来,您只能访问提交的属性哈希,例如检查字
我知道你可以拥有: accepts_nested_attributes_for :foo, :reject_if => proc { |a| a[:bar].blank? } 有没有办法代替说类似的话
在我的模型中,我有 has_one :order, dependent: :destroy accepts_nested_attributes_for :order, reject_if: lambd
我被reject_if拒绝了。 Item 模型 has_many 变体,该模型也接受嵌套属性: accepts_nested_attributes_for :variants, :allow_de
我正在处理一些涉及上传图片的表单。有一个标准的两个表单要添加以及当前关联的所有表单。它看起来像这样: 我有一个 Assets 类,它对于涉及的其他类(例如位置、项目)是多态的。问题是可以上传或更新项目
我正在尝试在下面创建一个自定义版本 accepts_nested_attributes_for :categories, :reject_if => proc { |hash| hash['tit
我有两个模型在 rails 上。首先是患者模型 class Patient attr_accessible :name, :age, :sex, :female_attributes has_one
我是 Rails 的新手,因此非常感谢任何建议。 我有一个带有嵌套属性地址的类条目, /app/models/entry.rb class Entry :destroy accepts_nest
所以我有以下模型 宠物 class Pet :all_blank end 拥有者 class Owner :callable, :dependent => :destroy has_one :
因此,我有一个Profile对象的许多多态子对象。如果特定对象的字段为空,则这些模型旨在销毁after_save。 但是对于accept_nested_attributes,如果它为空,我不想首先创建
我是一名优秀的程序员,十分优秀!