0

I am creating something very similar to this question. However, I receive the following error.(Ignore the fact that there's an employee_email and email, the user's email will become username)

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

I found this post in regards to user exist error, but I don't see where it would be failing a validation. The email for the user table doesn't exist, and the password meet minimum requirement. What am I missing/overlooking?

Models

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

Controller

        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

View

            <%= 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 %>
Needtoknow
  • 5
  • 1
  • 5

1 Answers1

0

The problem is this line before_action :authenticate_user!. Can you comment it out and see if the error goes away. Devise was trying to authenticate your newly built user(nil email), hence the validation error.

kasperite
  • 2,460
  • 1
  • 15
  • 17
  • Still get the same issue, I've gone through commented the before_action :authenticate_user! in every controller, and restarted the app/server just to make sure – Needtoknow Apr 16 '18 at 00:29
  • You probably want to tweak your `create` action to match with https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb too, since you are creating a new user here. – kasperite Apr 16 '18 at 01:09