法术用户注册错误,无效属性password_confirmation

时间:2016-03-15 19:51:02

标签: ruby-on-rails ruby ruby-on-rails-4

我一直收到错误提示无效的属性password_confirmation。我无法弄清楚我做错了什么。谁能解释一下需要在巫术用户控制器中传递哪些安全参数来创建用户?

错误我一直

Failures:

  1) Users can sign Up With Valid attributes
     Failure/Error: @user = User.new(user_params)

     ActiveRecord::UnknownAttributeError:
       unknown attribute 'password_confirmation' for User.
     # ./app/controllers/users_controller.rb:8:in `create'
     # ./spec/feature/sign_up_spec.rb:27:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # NoMethodError:
     #   undefined method `password_confirmation=' for #<User:0x00563958734578>
     #   ./app/controllers/users_controller.rb:8:in `create'

我的控制器

    class UsersController < ApplicationController 

layout 'static_application' 

   def new
      @user = User.new  
   end

   def create
      @user = User.new(user_params)
      if @user.save
         flash[:notice] = 'Thank you for Applying for PetroHub account. We are currently reviewing your application, which might take upto 24hrs.'
         redirect_to user_thankyou_path(@user)
      else
         flash[:alert] = 'Oops! something went wrong. Please check your application and resubmit. Thank you.'
       end   
    end

      def thank_you
        @user = User.find(params[:id])   
      end

      private

      def user_params
      params.require(:user).permit(:first_name,:last_name,:business_name,:phone_number,:cell_number,:street_address,:apt_suite,:city,:state,:zip_code,:tax_id,:ssn,:in_biz,:terms,:email,:password,:password_confirmation)
    end 
  end

我的规格

require 'rails_helper'

RSpec.feature 'Users can sign Up' do
  scenario 'With Valid attributes' do
    visit '/'
    click_link 'apply'

    expect(page.current_url).to eq signup_url

    fill_in 'First Name', with: 'John'
    fill_in 'Last Name', with: 'Doe'
    fill_in 'Business Name', with: 'Acme Inc'
    fill_in 'Phone Number', with: '2124567890'
    fill_in 'Cell Number', with: '5202124567'
    fill_in 'Street Address', with: '212 Wise St'
    fill_in 'Apt/Suite', with: '410'
    fill_in 'City', with: 'New york'
    select "New York", :from => "State"
    fill_in 'Zip Code', with: '11104'
    fill_in 'Tax ID', with: '12345678'
    fill_in 'SSN', with: '124000987'
    fill_in 'How many years in Business?', with: '5'
    fill_in 'Email', with: 'test@example.com'
    fill_in 'user_password', with: 'password'
    fill_in 'user_password_confirmation', with: 'password'
    check('user_terms')
    click_button 'Apply'


    expect(page).to have_content 'Thank you for Applying for PetroHub account. We are currently reviewing your application, which might take upto 24hrs.'
  end

  scenario 'With Invalid attributes' do
  end
end

1 个答案:

答案 0 :(得分:1)

validates_confirmation_of :password添加到模型中。这样会在模型对象中创建一个属性。

但是,请注意,这使得对模型对象密码的所有编辑都需要确认,包括在Rails控制台中。最好在您的控制器或服务对象中执行确认。