任何对 Rails 4 有更多经验的人都可以帮助我。我目前正在阅读“Rails 4 in Action”,并且无法通过书中的一个规范(特别是第 7.5 章:基本访问控制——基于命名空间的 CRUD)。我有一个规范,它创建一个具有管理员权限的 FactoryGirl 用户,并尝试创建一个具有管理员权限的新用户。
spec/features/admin/creating_users_spec.rb
require 'spec_helper'
feature "Creating Users" do
let!(:admin) { FactoryGirl.create(:admin_user) }
before do
sign_in_as!(admin)
visit '/'
click_link "Admin"
click_link "Users"
click_link "New User"
end
scenario 'Creating a new user' do
fill_in "Email", with: "newbie@example.com"
fill_in "Password", with: "hunter2"
click_button "Create User"
expect(page).to have_content("User has been created.")
end
end
当我运行此规范时,我相信我不断收到“密码确认不能为空白错误”。
`$ bin/rspec spec/features/admin/creating_users_spec.rb`
DEPRECATION WARNING: You didn't set config.secret_key_base. Read the upgrade
documentation to learn more about this new config option. (called from get at
/home/sean/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/forwardable.rb:171)
F
Failures:
1) Creating Users Creating a new user
Failure/Error: expect(page).to have_content("User has been created.")
expected there to be text "User has been created." in "Signed in as: username Admin Sign out User has not been created. New User 1 error prohibited this user from being saved: Password confirmation can't be blank Email Password Is an admin?"
# ./spec/features/admin/creating_users_spec.rb:19:in `block (2 levels) in <top (required)>'
Finished in 2.82 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/admin/creating_users_spec.rb:15 # Creating Users Creating a new user
Randomized with seed 56202
这是创建它的工厂:
FactoryGirl.define do
sequence(:email) {|n| "user#{n}example.com" }
factory :user do
name "username"
email { generate(:email) }
#email "newbie@example.com"
password "hunter2"
password_confirmation "hunter2"
factory :admin_user do
admin true
end
end
end
这是管理控制器:
class Admin::UsersController < Admin::BaseController
def new
@user = User.new
end
def index
@users = User.order(:email)
end
def create
@user = User.new(user_params)
if @user.save
flash[:notice] = "User has been created."
redirect_to admin_users_path
else
flash.now[:alert] = "User has not been created."
render action: "new"
end
end
private
def user_params
params.require(:user).permit(:name, :password, :password_confirmation)
end
end
这是用户模型:
class User < ActiveRecord::Base
attr_accessible :name, :password, :password_confirmation
has_secure_password
end
最后是填写所有这些信息的管理视图表单:
<%= form_for [:admin, @user] do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited
this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password %>
</p>
<p>
<%= f.check_box :admin %>
<%= f.label :admin, "Is an admin?" %>
</p>
<%= f.submit %>
<% end %>
我不知道如何解决这个问题。我尝试在表单中创建另一个字段以确认密码,然后在规范中包含一个 fill_in,但这没有做任何事情。我觉得这与具有某种限制的模型有关,除非有密码确认,否则不允许创建用户。但我不太确定。如果有人可以提供帮助,我将不胜感激。