happycoffeebean
happycoffeebean

Reputation: 43

'Add New User' works in Development, not in Production

Background: 'Add User' portion of an app works perfectly on two separate development machines, running two different OSes: Dev Machine 1 is a Mac running Catalina (10.15), Dev machine 2 is running an Ubuntu derivative (Pop!_OS 19.10). After pushing to a Digital Ocean Dokku server the 'Add User' functionality fails without error; hitting the submit button does nothing. 'Edit User' works in production, as well as dev.

The code is being managed via git, with no complications on that part. Therefore, the identical code is on three machines.

I rely on Devise for my user setup; however, skip registrations in order to restrict the public from creating an account. Instead I rely on cancancan with a role to create users. This all works in development, but I cannot see a proper error in production, it simply does a render :action => :new with no explicit error. I use Dokku, Rails 6.03 with Ruby Version 2.7.1

Development Log

 Started POST "/user_managements" for ::1 at 2021-01-23 13:30:48 -0500
Processing by UsersController#create as JS
  Parameters: {"authenticity_token"=>"zr4lstDZAfXC3dTf1UADmoa6jbZl5HNQZCuDDdnWBuqa32LtAnadmjYHN+PnVUfs6RFVwcJ03KtpaE/l5osdiQ==", "user"=>{"email"=>"[email protected]", "first_name"=>"Alice", "last_name"=>"Wonderland", "phone_number"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "superadmin_role"=>"0", "supervisor_role"=>"0", "user_role"=>"1"}, "commit"=>"Submit"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
   (0.4ms)  SELECT COUNT(*) FROM "stores"
  ↳ app/controllers/application_controller.rb:33:in `set_search'
   (0.2ms)  BEGIN
  ↳ app/controllers/users_controller.rb:22:in `create'
  User Exists? (0.4ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
  ↳ app/controllers/users_controller.rb:22:in `create'
  User Create (0.4ms)  ***INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at", "first_name", "last_name", "phone_number") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["email", "[email protected]"], ["encrypted_password", "$2a$11$VNfiMFc2CrDrdtsa31wXkOyhz4D4TkPVTiyDfceO62UwfZdS9fAhS"], ["created_at", "2021-01-23 18:30:49.138574"], ["updated_at", "2021-01-23 18:30:49.138574"], ["first_name", "Alice"], ["last_name", "Wonderland"], ["phone_number", ""]]***
  ↳ app/controllers/users_controller.rb:22:in `create'
   (9.6ms)  COMMIT
  ↳ app/controllers/users_controller.rb:22:in `create'
Redirected to http://localhost:3000/user_managements
Completed 200 OK in 257ms (ActiveRecord: 11.3ms | Allocations: 8092)

Production Log (Note the absence of the "INSERT" statement.)

 Started POST "/user_managements" for 173.170.223.213 at 2021-01-23 17:35:02 +0000
2021-01-23T17:35:02.954032036Z app[web.1]: I, [2021-01-23T17:35:02.953856 #10]  INFO -- : [6c93c1d9-2122-4cda-8f39-ed32e42b39e0] Processing by UsersController#create as JS
2021-01-23T17:35:02.954859299Z app[web.1]: I, [2021-01-23T17:35:02.954744 #10]  INFO -- : [6c93c1d9-2122-4cda-8f39-ed32e42b39e0]   Parameters: {"authenticity_token"=>"IlDwRv3r8Qv51qXyg+roq5FiuJ+pbJjsZTlWjq50hxVKtGHptRSusL7Slf0oNiqqwN1VADhGrSbdt7GZVMp9rg==", "user"=>{"email"=>"[email protected]", "first_name"=>"", "last_name"=>"", "phone_number"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "superadmin_role"=>"0", "supervisor_role"=>"0", "user_role"=>"1"}, "commit"=>"Submit"}
2021-01-23T17:35:02.959242127Z app[web.1]: D, [2021-01-23T17:35:02.959108 #10] DEBUG -- : [6c93c1d9-2122-4cda-8f39-ed32e42b39e0]   User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
2021-01-23T17:35:02.964234692Z app[web.1]: D, [2021-01-23T17:35:02.964099 #10] DEBUG -- : [6c93c1d9-2122-4cda-8f39-ed32e42b39e0]    (0.5ms)  SELECT COUNT(*) FROM "stores"
2021-01-23T17:35:02.983197832Z app[web.1]: I, [2021-01-23T17:35:02.982997 #10]  INFO -- : [6c93c1d9-2122-4cda-8f39-ed32e42b39e0]   Rendering users/new.html.erb within layouts/application
2021-01-23T17:35:02.985854583Z app[web.1]: I, [2021-01-23T17:35:02.985747 #10]  INFO -- : [6c93c1d9-2122-4cda-8f39-ed32e42b39e0]   Rendered users/new.html.erb within layouts/application (Duration: 1.7ms | Allocations: 905)
2021-01-23T17:35:02.989341142Z app[web.1]: I, [2021-01-23T17:35:02.989236 #10]  INFO -- : [6c93c1d9-2122-4cda-8f39-ed32e42b39e0] Completed 200 OK in 34ms (Views: 6.6ms | ActiveRecord: 1.0ms | Allocations: 4898)

=== relevant code begins below ===

Routes

    resources :paper_routes

  get 'stores/active/' => 'stores#active'
  get 'stores/inactive/' => 'stores#inactive'

  resources :stores do
    post :update_row_order, on: :collection
    resources :sheets
    collection do
    match 'search' => 'stores#search', via: [:get, :post], as: :search
    end
  end

  resources :user_managements, :controller => 'users'

  devise_for :users, :skip => [:registrations], controllers: { sessions: "users/sessions", passwords: "users/passwords" }

  as :user do
    root 'users/sessions#new'
    get 'users/edit' => 'users/registrations#edit', :as => 'edit_user_registration'
    put 'user' => 'users/registrations#update', :as => 'user_registration'
  end

 get 'dashboard' => 'home#index', as: 'dashboard'
 get 'reports' => 'reports#index', as: 'reports'
end

controllers/users_controller.rb

    def new
    @user = User.new
        respond_to do |format|
        format.html 
        format.json
      end
  end

  def create
    @user = User.new(user_params)
            if @user.save 
              flash[:notice] = 'User created'
              redirect_to user_managements_path and return
            else
              flash.now[:warning] = 'There were problems when trying to create a new User'
              render :action => :new
            end
   end

  def edit
    @user = User.find params[:id]
    respond_to do |format|
        format.html 
        format.json
      end
  end

 def update
  @user = User.find params[:id]
      if @user.update_attributes user_params
            flash[:notice] = 'User has been updated'
            redirect_to user_managements_path and return
        else
            flash.now[:warning] = 'There were problems when trying to update this User'
            render :action => :edit
        end
 end

(...)

 def user_params
      params.require(:user).permit(:id, :email, :password, :password_confirmation, :superadmin_role, :supervisor_role, :user_role, :first_name, :last_name, :phone_number)
 end

/views/users/new.html.erb

<%= form_with model: @user, url: user_managements_path, class: "form-group" do |f| %>
<div class="row pt-3">
  <div class="col-sm-12 col-md-4">
    <div class="field">
  <%= label_tag(:name, "Email") %>
  <%= f.text_field :email, :class => "form-control", :type => "text" %>
    </div>
  </div>
</div>
(...)

/views/users/edit.html.erb

<%= form_with model: @user, url: user_management_path(@user), class: "form-group" do |f| %>

<div class="row pt-3">
  <div class="col-sm-12 col-md-4">
    <div class="field">
  <%= label_tag(:name, "Email") %>
  <%= f.text_field :email, :class => "form-control", :type => "text" %>
    </div>
  </div>
</div>
(...)

Upvotes: 0

Views: 159

Answers (1)

happycoffeebean
happycoffeebean

Reputation: 43

If anyone has any insight as to why this worked in development but not production, I'd love to know. Seems like it had to do with not loading Devise first.

The original code in routes.rb:

resources :user_managements, :controller => 'users'
devise_for :users, :skip => [:registrations], controllers: { sessions: "users/sessions", passwords: "users/passwords" }

I switched the order around to:

devise_for :users, :skip => [:registrations], controllers: { sessions: "users/sessions", passwords: "users/passwords" }
resources :user_managements, :controller => 'users'

Which solved the problem. Thank you again, @idmean, @arieljuod, @max and @jim!

Upvotes: 1

Related Questions