我正在关注 Micheal Hartl 的 Ruby on Rails 教程书。我正在验证 sample_app 中是否存在名称和电子邮件。在 rails 控制台中,我正在运行
user = User.new(:email => "user@example.com")
测试一个缺席的名称值,但我得到了回报;
attr_accessible is extracted out of Rails into a gem.
Please use new recommended protection model for params(strong_parameters)
or add 'protected_attributes' to your Gemfile.
我的 user.rb 是,
class User < ActiveRecord::Base
attr_accessible :name, :email
validates :name, :presence => true
validates :email, :presence => true
end
因为我不确定如何使用 strong_parameters 我将 protected_attributes 添加到我的 Gemfile 中,就像这样,
source 'https://rubygems.org'
gem 'protected_attributes'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.2'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :test do
gem 'rspec'
end
group :development do
gem 'rspec-rails'
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
我怎样才能让我的验证工作?