HowToEnsureValidAttributesInFormData


attr_accessibleと可変長引数の話


http://wiki.rubyonrails.org/rails/pages/HowToEnsureValidAttributesInFormData
ちょっと記事が古いなぁ。今なら違う方法でできるかも。

# @params['user'] contains the attributes we want to add

# allowed_attr specifies which attributes are allowed
if some_flag
  allowed_attr = ['name','email','password','status','balance']
else
  allowed_attr = ['name','email','password']
end

# use delete_if so the hash only contains valid attributes
@params['user'].delete_if {|k,v| not allowed_attr.include? k}

# mass assign our cleaned attributes to the object
user = User.new(@params['user'])


active_supportの力があれば!!1

# @params['user'] contains the attributes we want to add

# allowed_attr specifies which attributes are allowed
if some_flag
  allowed_attr = ['name','email','password','status','balance']
else
  allowed_attr = ['name','email','password']
end

# mass assign our cleaned attributes to the object
user = User.new(@params['user'].slice(*allowed_attr)) #←ここに注目
              • -

ぼくも早く2.2に上げて
Object#present?でびゅーしてunlessからおさらばしたいです。
unless 〜 else とか分かりにくいからやめてほしいよ><