Rails 4 の経験が豊富な人なら誰でも私を助けてくれませんか? 私は現在、「Rails 4 in Action」を進めていますが、本の仕様の 1 つを渡すのに苦労しています (具体的には、7.5 章から: Basic Access control -- Namespaced-based 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 を含めましたが、何もしませんでした。パスワードの確認がない限り、ユーザーの作成を許可しない、何らかの制限があるモデルと関係があるように感じます。しかし、よくわかりません。誰かが助けてくれれば、本当に感謝しています。