Search code examples
ruby-on-rails-4deploymentpassengerdevelopment-environmentproduction-environment

Rails: changing from development to production leads to NameError uninitialized constant


My application works in dev environment (WEBrick 1.3.1) on my mac laptop. I deployed it via capistrano onto a Ubuntu server running nginx & passenger and suddenly I am getting

NameError in SmsController#send_text_message: uninitialized constant SmsController::PhoneNumber

. Here is a screenshot:

enter image description here

Apparently, the PhoneNumber class (app/models/phone_number.rb) is not being recognized. Here is the code for that class:

class PhoneNumber
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :pnumber

  validates :pnumber, presence: true 
  validates :pnumber, numericality: true

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end

Here is the code for the controller where the error is raised:

class SmsController < ApplicationController
  def send_text_message
    phone = PhoneNumber.new(pnumber: params[:phone]) 
    logger.info "phone as submitted by the webform (params[:phone]) = " + params[:phone]
    if phone.valid?
      # code that sends a sms message..
      flash[:success] = "Message has been sent!"
      redirect_to :back
    else
      flash[:warning] = "This is not a valid mobile number" 
      redirect_to :back
    end
  end
end

What do I need to do to make this work in production?

==EDIT: I ran it locally on my mac under the production environment using the same stack (nginx, passenger) and I am not getting the error message. So it seems it must be something specific to the installation on my Ubuntu VPS. I re-started nginx but it did not change anything. I am really stumped - in theory this quirk should not happen.

==EDIT2: As requested by @rossta here is the content of config/application.rb:

require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Appmate
  class Application < Rails::Application
  # Settings in config/environments/* take precedence over those specified here.
  # Application configuration should go into files in config/initializers
  # -- all .rb files in that directory are automatically loaded.
end
end

And here is the content of config/environments/production.rb:

Appmate::Application.configure do
config.cache_classes = true   a
config.eager_load = true

# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local       = true # changed to help with debugging, TODO: change back to false once in production
config.action_controller.perform_caching = true

# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
# config.action_dispatch.rack_cache = true

# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = false

# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass

# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false

# Generate digests for assets URLs.
config.assets.digest = true

# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'
config.log_level = :info
config.i18n.fallbacks = true

# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify

# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false

# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
end

Solution

  • I found the culprit - I seem to have not committed to my git repository the controller and the model files. So everything was working apart from that bit. Duh!

    @rossta: thanks for helping! That stray 'a' is something that I must have added while copying the code into the SO post but your question made me look into my git repository - and this is how I found the changed but uncommitted files.