2

任何对 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,但这没有做任何事情。我觉得这与具有某种限制的模型有关,除非有密码确认,否则不允许创建用户。但我不太确定。如果有人可以提供帮助,我将不胜感激。

4

2 回答 2

0

我尝试在表单中创建另一个字段以确认密码,然后在规范中包含一个 fill_in,但这没有做任何事情。

把它放回去,然后删除 User 模型中的整attr_accessible行。

在 Rails 4 中,默认情况下允许大量分配的属性作为其强参数策略的一部分——您不必在模型中显式地将属性列入白名单。

如果这不起作用,请发布包含新password_confirmation表单字段和规范的更新视图。

于 2013-07-26T00:39:47.867 回答
0

那行得通!

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)
.

Finished in 0.44082 seconds
1 example, 0 failures

Randomized with seed 45288

这是规格:

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

#=begin
  scenario 'Creating a new user' do
    fill_in "Email", with: "newbie@example.com"
    fill_in "user[password]", with: "hunter2"
    fill_in "Password confirmation", with: "hunter2"
    click_button "Create User"
    expect(page).to have_content("User has been created.")
  end
#=end
=begin
  scenario "Creating an admin user" do
    fill_in "Email", with: "admin@example.com"
    fill_in "Password", with: "password"
    check "Is an admin?"
    click_button "Create User"
    expect(page).to have_content("User has not been created")
    within("#users") do
      expect(page).to have_content("admin@example.com (Admin)")
    end
  end
=end
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.label :password_confirmation %>
    <%= f.password_field :password_confirmation %>
  </p>
  <p>
    <%= f.check_box :admin %>
    <%= f.label :admin, "Is an admin?" %>
  </p>
    <%= f.submit %>
<% end %>

虽然当我在用户模型中注释掉 attr_accessible 时,我得到了一个错误:

class User < ActiveRecord::Base
  #attr_accessible :name, :password, :password_confirmation

  has_secure_password
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: click_button "Create User"
     ActiveModel::MassAssignmentSecurity::Error:
       Can't mass-assign protected attributes for User: password, password_confirmation
     # ./app/controllers/admin/users_controller.rb:11:in `create'
     # ./spec/features/admin/creating_users_spec.rb:19:in `block (2 levels) in <top (required)>'

Finished in 0.45317 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 7339

所以也许当我上次尝试创建密码确认字段时,我可能遗漏了一些东西,而这次我没有——所以它起作用了。

万分感谢!

于 2013-07-26T01:44:26.967 回答