密码确认不能为空

时间:2013-07-26 00:24:09

标签: ruby-on-rails controller factory-bot password-confirmation

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

2 个答案:

答案 0 :(得分:0)

  

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

将其放回,然后删除用户模型中的整个attr_accessible行。

Rails 4中默认允许使用大量分配的属性作为其strong parameters策略的一部分 - 您不必在模型中明确地将属性列入白名单。

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

答案 1 :(得分: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

所以也许当我最后一次尝试创建密码确认字段时,我可能会遗漏一些东西,这次我没有 - 所以它有效。

非常感谢!