方法
实例公共方法
assign_attributes(new_attributes) 链接
允许你通过传入一个散列的属性(键与属性名匹配)来设置所有属性。
如果传入的散列对permitted?
方法有响应,并且此方法的返回值为false
,则会引发ActiveModel::ForbiddenAttributesError
异常。
class Cat
include ActiveModel::AttributeAssignment
attr_accessor :name, :status
end
cat = Cat.new
cat.assign_attributes(name: "Gorby", status: "yawning")
cat.name # => 'Gorby'
cat.status # => 'yawning'
cat.assign_attributes(status: "sleeping")
cat.name # => 'Gorby'
cat.status # => 'sleeping'
别名:attributes=
# File activemodel/lib/active_model/attribute_assignment.rb, line 28 def assign_attributes(new_attributes) unless new_attributes.respond_to?(:each_pair) raise ArgumentError, "When assigning attributes, you must pass a hash as an argument, #{new_attributes.class} passed." end return if new_attributes.empty? _assign_attributes(sanitize_for_mass_assignment(new_attributes)) end