Christopher Warrington
Christopher Warrington

Reputation: 767

ActionController::RoutingError (uninitialized constant Members) in Production Only

I have read through so many different questions posted, and can't find my answer. It seems that everyone else was having this issue in Development and it was due to naming issues. Everything works fine for me in development, but errors in production.

WebApp Info:

Rails: 5.1.4

Ruby: 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17]

Deployed To: Heroku

Error received:

2018-02-12T17:59:43.693569+00:00 heroku[router]: at=info method=GET path="/login" host=the-christian-chain.herokuapp.com request_id=9933c1eb-5023-4814-976d-a656053794d3 fwd="137.152.185.249" dyno=web.1 connect=1ms service=5ms status=404 bytes=1902 protocol=https
2018-02-12T17:59:43.690742+00:00 app[web.1]: I, [2018-02-12T17:59:43.690635 #4] INFO  -- : [9933c1eb-5023-4814-976d-a656053794d3] Started GET "/login" for 137.152.185.249 at 2018-02-12 17:59:43 +0000
2018-02-12T17:59:43.692591+00:00 app[web.1]: F, [2018-02-12T17:59:43.692522 #4] FATAL -- : [9933c1eb-5023-4814-976d-a656053794d3]   
2018-02-12T17:59:43.692655+00:00 app[web.1]: F, [2018-02-12T17:59:43.692591 #4] FATAL -- : [9933c1eb-5023-4814-976d-a656053794d3] ActionController::RoutingError (uninitialized constant Members):
2018-02-12T17:59:43.692715+00:00 app[web.1]: F, [2018-02-12T17:59:43.692656 #4] FATAL -- : [9933c1eb-5023-4814-976d-a656053794d3]   
2018-02-12T17:59:43.692815+00:00 app[web.1]: F, [2018-02-12T17:59:43.692738 #4] FATAL -- : [9933c1eb-5023-4814-976d-a656053794d3] vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:269:in `const_get'
....

Then it lists off a bunch of other requests until final GET of homepage.

I had an issue where the app would crash after deploying it to Heroku with the same uninitialized constant Members type issue. So, I changed the eager_load to false, which seemed to fix that issue. But, now seeing this, I am wondering if there is a bigger issue in my code.

Changed that to false here: config/environments/production.rb

....
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = false
....

Section of my routes file with devise routing:

config/routes.rb

######################
###    Members     ###
######################

  devise_for :members, 
              controllers: {
                sessions: 'members/sessions',
                registrations: 'members/registrations'
              },
              path: '', path_names: { 
                  sign_in:    'login', 
                  sign_out:   'logout', 
                  sign_up:    'register',
                  edit:       ':username/account/edit'
              }

I can confirm that the customized Devise controllers are nested correctly and spelling is correct there as login and registration all works locally in development.

Here is the result for the MembersController when I run heroku rake routes

        new_member_session GET    /login(.:format)                       members/sessions#new
            member_session POST   /login(.:format)                       members/sessions#create
    destroy_member_session DELETE /logout(.:format)                      members/sessions#destroy
       new_member_password GET    /password/new(.:format)                devise/passwords#new
      edit_member_password GET    /password/edit(.:format)               devise/passwords#edit
           member_password PATCH  /password(.:format)                    devise/passwords#update
                           PUT    /password(.:format)                    devise/passwords#update
                           POST   /password(.:format)                    devise/passwords#create
cancel_member_registration GET    /cancel(.:format)                      members/registrations#cancel
   new_member_registration GET    /register(.:format)                    members/registrations#new
  edit_member_registration GET    /:username/account/edit(.:format)      members/registrations#edit
       member_registration PATCH  /                                      members/registrations#update
                           PUT    /                                      members/registrations#update
                           DELETE /                                      members/registrations#destroy
                           POST   /                                      members/registrations#create

app/models/member.rb

class Member < ApplicationRecord
  has_one :profile, dependent: :destroy
  has_many :involvements, dependent: :destroy
  has_many :donations

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  accepts_nested_attributes_for :profile

  validates_uniqueness_of :email
end

Members Controller

app/controllers/Members/sessions_controller.rb

class Members::SessionsController < Devise::SessionsController
  # before_action :configure_sign_in_params, only: [:create]

  # GET /resource/sign_in
  def new
    if params[:return_page]
      session[:member_return_to] = params[:return_page]
    end

    if params[:add_support]
      flash[:warning] = 'You need to login or register for an account first.'
      session[:add_support] = params[:add_support]
      @support = Support.find_by(url_friendly_title: params[:add_support])
      session[:member_return_to] = support_page_path(@support.url_friendly_title)
    end

    if params[:return_page] || params[:add_support]
      redirect_to new_member_session_path
      return
    end

    super
  end

  # POST /resource/sign_in
  # def create
  #   super
  # end

  # DELETE /resource/sign_out
  # def destroy
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_in_params
  #   devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
  # end
end

Help Please

Any help that you can give on why the Devise model for Members is not being initialized on Heroku would be very much appreciated. Did I forget some setting that needs to be changed when deploying?

Upvotes: 2

Views: 2041

Answers (1)

Christopher Warrington
Christopher Warrington

Reputation: 767

After much research, this error can be caused for a host of reasons. I will post the answer that fixed my particular issue that caused this error, in case someone in the future has the same issue.

Now, on to fix the last problems before launching my site tomorrow. Thank you all for your help in tracking down the issue.

Answer:

If you look in my question, the sessions_controller.rb was nested under 'Members' not 'members'. As @max pointed out in his clarifying comments, the file system on my local machine is case-insensitive, where as in production it is case sensitive. So, that is why "Members" was not initialized.

Upvotes: 1

Related Questions