【发布时间】:2013-07-13 21:44:22
【问题描述】:
我以前在模型中使用的导轨 4
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
...
end
但现在strong_parameters 替换了protected_attributes,所以我评论它并使用permit。
现在我发现我可以在未经允许的情况下访问属性。
在rails c 我设法做到了:
2.0.0p247 :002 > User.new(admin: "1")
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil, password_digest: nil, remember_token: nil, admin: true>
2.0.0p247 :016 > user = User.new(name: 'Nir', email: 'nir@example.com', password: 'foobar', password_confirmation: 'foobar', admin: "1")
=> #<User id: nil, name: "Nir", email: "nir@example.com", created_at: nil, updated_at: nil, password_digest: "$2a$10$xVnY8ydd5SoaLVipK5j4Del40FrOmu4bKypGjBEwvms7...", remember_token: nil, admin: true>
显然我不应该能够设置和更改管理属性。只有user.toggle(:admin) 应该可以。
那么我不理解或应该做对什么。 以及如何使这个测试通过:
describe "accessible attributes" do
it "should not have allow access to admin" do
expect do
User.new(admin: "1")
end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 rspec ruby-on-rails-4