![](/img/trans.png)
[英]Railstutorial 6.3.5 Can't mass-assign protected attributes: password, password_confirmation
[英]Rspec for Devise: Can't mass-assign protected attributes: user_password, user_password_confirmation
我正在嘗試使用RSpec為Devise 2.2.2編寫注冊規范 。 我像往常一樣准備了User
模型。
# app/models/user.rb
class User < ActiveRecord::Base
attr_accessible :role_ids, :as => :admin
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
rolify
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:token_authenticatable
after_create :add_default_role
def add_default_role
add_role :user
end
end
在規范中,我需要使用password
和password_confirmation
注冊user
,如下所示。
# spec/requests/users/sign_up_spec.rb
describe 'Sign up' do
before(:each) do
sign_out
end
it 'signs up a visitor with valid data' do
user = build(:user)
click_link I18n.t('layouts.navigation.sign_up')
fill_in I18n.t('devise.registrations.new.name'), with: user.name
fill_in I18n.t('devise.registrations.new.email'), with: user.email
find('.js-password').set user.password
find('.js-password-confirmation').set user.password_confirmation
click_button I18n.t('devise.registrations.new.submit')
expect(page).to have_content I18n.t('layouts.navigation.sign_out')
expect(page).not_to have_content I18n.t('layouts.navigation.sign_up')
expect(page).not_to have_content I18n.t('layouts.navigation.sign_in')
end
end
以下是用於呈現頁面的Devise視圖模板:
<!-- app/views/devise/registrations/new.html.erb -->
<h2><%= t('.title', :default => "Sign up") %></h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :name, t('.name', :default => "Name" ) %><br />
<%= f.email_field :name %></div>
<div><%= f.label :email, t('.email', :default => "Email" ) %><br />
<%= f.email_field :email %></div>
<div><%= f.label :user_password, t('.user_password', :default => "Password" ) %><br />
<%= f.password_field :user_password, class: 'js-password' %></div>
<div><%= f.label :user_password_confirmation, t('.user_password_confirmation', :default => "Password confirmation" ) %><br />
<%= f.password_field :user_password_confirmation, class: 'js-password-confirmation' %></div>
<div><%= f.submit t('.submit', :default => "Submit") %></div>
<% end %>
雖然我將這兩個屬性添加到attr_accessible
, attr_accessible
這個錯誤消息卻拋向我:
Sign up signs up a visitor with valid data
Failure/Error: click_button I18n.t('devise.registrations.new.submit')
ActiveModel::MassAssignmentSecurity::Error:
Can't mass-assign protected attributes: user_password, user_password_confirmation
# ./spec/requests/users/sign_up_spec.rb:20:in `block (2 levels) in <top (required)>'
表單中輸入的名稱是錯誤的,應該是user[password]
和user[password_confirmation]
,但錯誤是說你有user_password
和user_password_confirmation
可以在此處查看視圖模板中的相關更改:
<div><%= f.label :password, t('.password', :default => "Password" ) %><br />
<%= f.password_field :password, class: 'js-password' %></div>
<div><%= f.label :password_confirmation, t('.password_confirmation', :default => "Password confirmation" ) %><br />
<%= f.password_field :password_confirmation, class: 'js-password-confirmation' %></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.