Home Rails app fails with "uninitialized constant"
Reply: 0

Rails app fails with "uninitialized constant"

user2243
1#
user2243 Published in May 22, 2018, 11:46 am

I could use some help figuring out a problem in a trivial Rails app (version 4, I believe). I'm not familiar with Rails, the app is just part of a Docker/AWS tutorial that I'm stepping through. I need to get it working so the following steps make sense. It's a little surprising that the app gets an error, because I pasted it in directly from the course resources.

When I load the main page, I get an error page something like this:

    NameError in PagesController#home
    uninitialized constant PagesController::ThrowJavelinsJob
    Extracted source (around line #6):

    @hostname   = Socket.gethostname

    ThrowJavelinsJob.perform_later
  end

  def health_check

This is in "./app/controllers/pages_controller.rb" from the root of the application:

class PagesController < ApplicationController
  def home
    @total_hits = Rails.cache.increment('total_hits')
    @hostname   = Socket.gethostname

    ThrowJavelinsJob.perform_later
  end

  def health_check
    head :ok
  end
end

In "./app/jobs/throw_javelins_job.rb" is the following:

class ThrowJavelinsJob < ActiveJob::Base
  queue_as :default

  def perform(*args)
    sleep 5
    javelins_thrown = rand(1..10)
    Javelin.create!(thrown: javelins_thrown)
  end
end

I did get a hint on gitter that I need to either "autoload" this module or "require" it. I don't know how I would autoload it, but in case making it "require" it would be done by a simple change to existing code, I grepped for "require" in the application code and got this:

./config/application.rb:1:require File.expand_path('../boot', __FILE__)
./config/application.rb:3:require 'rails/all'
./config/application.rb:7:Bundler.require(*Rails.groups)
./config/environment.rb:2:require File.expand_path('../application', __FILE__)
./config/boot.rb:3:require 'bundler/setup' # Set up gems listed in the Gemfile.
./Rakefile:4:require File.expand_path('../config/application', __FILE__)
./config.ru:3:require ::File.expand_path('../config/environment', __FILE__)

So, what would be the most logical change to this existing code to get this to work?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.771372 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO