【问题标题】:Devise user exists error, passing data from employee to user设计用户存在错误,将数据从员工传递给用户
【发布时间】:2018-04-15 23:09:01
【问题描述】:

我正在创建与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 %>

【问题讨论】:

  • create是否出现此错误?
  • 是的,还没有设置更新/编辑,因为我想在继续之前解决这部分问题。

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


【解决方案1】:

问题是这一行before_action :authenticate_user!。你能把它注释掉,看看错误是否消失。设计试图验证您新建的用户(无电子邮件),因此验证错误。

【讨论】:

  • 仍然遇到同样的问题,我已经通过评论 before_action :authenticate_user!在每个控制器中,并重新启动应用程序/服务器以确保
  • 您可能还想调整您的create 操作以匹配github.com/plataformatec/devise/blob/master/app/controllers/…,因为您在这里创建了一个新用户。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2012-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多