设计用户存在错误,将数据从员工传递给用户

时间:2018-04-15 23:09:01

标签: ruby-on-rails devise ruby-on-rails-5

我正在制作与this question非常相似的内容。但是,我收到以下错误。(忽略这样一个事实,即有一个employee_email和电子邮件,用户的电子邮件将成为用户名)

Parameters: {"utf8"=>"✓", "authenticity_token"=>"YpbYIJT1LiF3Wjv5ygnGSo4DqoGEP96csMqJwznALabEgoo4HyhQdKwpp1DRWG8yEVn/USo3BT/ABCZZZvlSIg==", "employee"=>{"store_id"=>"1", "employee_
name"=>"Geoffe Phranks", "employee_phone"=>"5741012569", "employee_email"=>"employee@employee.edu", "employee_street"=>"512 Five", "employee_city"=>"Portland", "employee_state"=>"CA"
, "employee_zip"=>"99874", "user_attributes"=>{"email"=>"admin3@admin.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "admin"=>"1"}}, "commit"=>"submit"}
  User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
   (0.0ms)  BEGIN
  Store Load (0.0ms)  SELECT  "stores".* FROM "stores" WHERE "stores"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  User Exists (0.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "admin3@admin.com"], ["LIMIT", 1]]
   (0.0ms)  ROLLBACK

我发现this post关于用户存在错误,但我不知道验证失败的地方。用户表的电子邮件不存在,密码满足最低要求。我错过了什么/忽视?

模型

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  belongs_to :employee
end

class Employee < ApplicationRecord
  has_many :orders
  has_one :user
  belongs_to :store

  accepts_nested_attributes_for :user
end

控制器

        class EmployeesController < ApplicationController

      before_action :authenticate_user!

      def new
        @employee = Employee.new
        @store = Store.all
        @employee.build_user
      end

      def create
        @employee = Employee.new(params.require(:employee).permit(:store_id, :employee_name, :employee_phone, :employee_email, :employee_street, :employee_city, :employee_state, :employee_zip, :employee_archive,
        user_attributes: [:email, :password, :password_confirmation, :admin]))

        if @employee.save
          redirect_to employees_path, :notice=> 'Employee was successfully created.'
        else
          redirect_to new_employee_path, :alert=> 'Employee was not created.'
        end
      end

      def edit
        @employee = Employee.find(params[:id])
        @store = Store.all
      end

      def update
        @employee = Employee.find(params[:id])

        if @employee.update(params.require(:employee).permit(:store_id, :employee_name, :employee_phone, :employee_email, :employee_street, :employee_city, :employee_state, :employee_zip, :employee_archive))
          redirect_to employees_path, :notice=> 'Employee was successfully updated.'
        else
          redirect_to edit_employees_path, :alert=> 'Employee was not updated.'
        end
      end

    end

查看

            <%= form_for @employee, :html=>{:class=>'form-horizontal push-10-t'} do|f| %>
        #
        #  Employee info fields
        #
 <%= f.fields_for :user do |u| %>
   <div class="form-group">
      <label class="col-xs-12">Login Email</label>
          <div class="col-sm-12">
                <%= u.email_field :email, :class=>"form-control empty" %>
                 </div>
                </div>
                <div class="form-group">
                 <label class="col-xs-12">Password</label>
                 <div class="col-sm-12">
                   <%= u.password_field :password, :class=>"form-control empty" %>
                    <% if @minimum_password_lenght %>
                      <br />
                     <em class="pull-right"><%= @minimum_password_lenght%> characters minimum</em>
                    <% end %>
                 </div>
                </div>
                <div class="form-group">
                 <label class="col-xs-12">Confirm Password</label>
                    <div class="col-sm-12">
                     <%= u.password_field :password_confirmation, :class=>"form-control empty" %>
                   </div>
                   </div>
                   <div class="form-group clearfix">
                    <div class="checkbox-custom checkbox-inline checkbox-primary pull-left">
                      <%= u.check_box :admin %>
                     <label for="user_admin">Admin</label>
                   </div>
                 </div>
               <% end %>                   
                <div class="form-group">
                 <div class="col-sm-12">
                   <%= f.submit :class=>'btn btn-sm btn-primary', :value=>'submit' %>
                 </div>
                </div>
             <% end %>

1 个答案:

答案 0 :(得分:0)

问题在于这一行before_action :authenticate_user!。你能评论一下,看看错误是否消失。 Devise试图验证您新建的用户(无电子邮件),因此验证错误。