ruby-on-rails - 无法批量分配 protected 属性

标签 ruby-on-rails cucumber

我的 features 文件是这样看的:

Given there are the following users:
    | email              | password | admin |
    | admin@ticketee.com | password | true  |

而且我的 user 模型没有将 admin 属性声明为 attr_accessible 以防止批量分配。因此,我对 user_steps.rb 文件进行了更改以解决此问题。
Given /^there are the following users:$/ do |table|
  table.hashes.each do |attributes|
   unconfirmed = attributes.delete("unconfirmed") == "true"
   @user = User.create!(attributes)
   @user.update_attribute("admin", attributes["admin"] == "true")
   @user.confirm! unless unconfirmed
 end
end

现在这应该根据书 - Rails3 in action 工作。我也检查了他们 online repo 上的代码。
用 cucumber 运行它会出现以下错误:
Can't mass-assign protected attributes: admin (ActiveModel::MassAssignmentSecurity::Error)
  ./features/step_definitions/user_steps.rb:4:in `block (2 levels) in <top (required)>'
  ./features/step_definitions/user_steps.rb:2:in `each'
  ./features/step_definitions/user_steps.rb:2:in `/^there are the following users:$/'
  features/creating_projects.feature:7:in `Given there are the following users:'

任何帮助将不胜感激。我真的无法弄清楚这里出了什么问题。

非常感谢!

最佳答案

在用户模型中添加:

attr_accessible :admin

更新:
admin 属性可以批量分配,任何黑客都可以通过发送参数轻松设置它。

关于ruby-on-rails - 无法批量分配 protected 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9742842/

相关文章:

ruby-on-rails - 将 --no-rdoc 和 --no-ri 与 bundler 一起使用

ruby-on-rails - Rails - 渲染部分而不必在文件名前使用 "_"?

ruby-on-rails - 在 Rails 3.1 中使用 rspec 测试嵌套资源 Controller

ruby-on-rails - 如何在 cucumber + capybara 步骤定义中设置 HTTP_REFERER?

java - java 中的 JUnit 测试

ruby-on-rails - 使用 Capybara-Webkit 监视 Javascript 函数

ruby-on-rails - 指定 rails 中种子文件的执行顺序

java - 是否可以在没有 JRuby/gems 的情况下开箱即用地在 Java 中运行 Cucumber?

cucumber - 如何在我的 cucumber BDD 步骤定义中使 "the"可选?

ruby-on-rails - Rails CANCAN - 第一次点击无访问权限