0

I'm following the Hartl book, Chapter 3, where we begin with static pages. We are told to do:

$ git checkout -b static-pages

No issues with that. Then, we are told to do the command:

$ rails generate controller StaticPages home help --no-test-framework

When I do that, I get this in the command line:

sample_app/config/application.rb:31:in `<top (required)>': undefined local variable or method `config' for main:Object (NameError)
from /Users/NormalUse/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.0.5/lib/rails/commands.rb:43:in `require'
from /Users/NormalUse/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.0.5/lib/rails/commands.rb:43:in `<top (required)>'
from bin/rails:8:in `require'
from bin/rails:8:in `<main>'

Line 31 of config/application.rb:31 is this:

config.assets.initialize_on_precompile = false

which I added because of Heroku issues. I tried taking out that line, and then it threw about 50 more errors.

Here is the gemfile we are supposed to be using

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.5'

group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end

group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end

gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'


group :doc do
gem 'sdoc', '0.3.20', require: false
end

group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end

Here is the full application.rb file:

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

# Pick the frameworks you want:
require "active_model/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
#require "rails/test_unit/railtie"

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

module SampleApp
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.

# Set Time.zone default to the specified zone and make Active Record auto-convert to     this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'

# The default locale is :en and all translations from config/locales/*.rb,yml are auto  loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
end
end
config.assets.initialize_on_precompile = false

Any ideas that I can try?

EDIT: After moving this: config.assets.initialize_on_precompile = false into the class like was suggested below, I get a lot of errors!

Here are the first 5 lines of errors: /Users/NormalUse/ror/sample_app/config/environments/development.rb:1:in `<top (required)>': undefined method `configure' for #<SampleApp::Application:0x007fa2178a73a0> (NoMethodError) from /Users/NormalUse/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `require' from /Users/NormalUse/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `block in require' from /Users/NormalUse/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:214:in `load_dependency' from /Users/NormalUse/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `require' from /Users/NormalUse/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.0.5/lib/rails/engine.rb:591:in `block (2 levels) in <class:Engine>'

EDIT EDIT: I just want to say thank you very much for those who helped me with this. I can't imagine how tough and frustrating it can be trying to help those of us who are REALLY new to all of this. I'm cruising through now that we've established that two different installed versions of Rails were conflicting.

Anthony Myers
  • 393
  • 1
  • 6
  • 18
  • Looks like there is an unwanted character ` in the config.Try erasing the line and type it with your own but don't copy. – Pavan Jun 05 '14 at 04:39
  • Within sample_app/config/environments/development.rb you mean? That is the original file that Rails New created. I haven't edited that one. – Anthony Myers Jun 05 '14 at 04:49

4 Answers4

3

Late to the party, but I (hopefully!) come bearing the answer: You almost certainly have both Rails 4.1 and Rails 4.0 installed. The format you're having trouble with is correct for Rails 4.1 and will be generated by Rails 4.1.

You can verify which version(s) are installed by running gem list --local rails.

So you've checked and seen you have both versions installed. What now? Do you have to uninstall one?

Nope! Simply specify the version number, wrapped in underscores, when you generate a new project:

rails _4.0.5_ new my_awesome_project

If no version number is specified, it will use the latest version available—thus your perfectly valid Rails 4.1 project that suddenly breaks when you replace the Gemfile.

colinm
  • 4,258
  • 24
  • 19
  • Thank you sir! I did/do have two versions installed. Like my own answer says though, since 4.1 defaults to `Rails.application.configure do`, instead of `SampleApp::Application.configure do`, I'm confused on that whole thing, but I guess that's for another question. – Anthony Myers Jun 06 '14 at 03:22
  • Glad it helped! When you change the Gemfile to specify `'rails','4.0.5'`, you're trying to use Rails 4.0.5 to run a Rails 4.1.x project (with new syntax it doesn't recognize, being Rails 4.0.5.). All Ruby knows about what to load is what the Gemfile tells it; if the Gemfile's wrong, things go boom. – colinm Jun 07 '14 at 01:49
1

What if you put it inside the class?

module SampleApp
  class Application < Rails::Application
    config.assets.initialize_on_precompile = false
  end
end
sawa
  • 165,429
  • 45
  • 277
  • 381
1

Try using:

gem 'rails', '4.0.4'

I actually skipped ahead and installed all the gems for the entire thing, which saved me time later.

Anthony Roberts
  • 1,971
  • 1
  • 19
  • 34
  • I just noticed that sawa provided the same solution as my edit. – Anthony Roberts Jun 05 '14 at 17:10
  • Thanks, Anthony, I will give that a shot and report back. – Anthony Myers Jun 05 '14 at 17:12
  • Try removing the initialize_on_precompile and see if that helps too. I'm not sure what you mean by Heroku issues, but I didn't need that to deploy on Heroku. – Anthony Roberts Jun 05 '14 at 17:15
  • Yeah, I had found that here in another question. Heroku was complaining about precompile. – Anthony Myers Jun 05 '14 at 18:36
  • No go, unfortunately. I tried both reverting to 4.0.4 and removing the precompile assets line, and I still get a ton of errors. The same errors. I guess I'm just going to start over without specifically using numbered versions of the gems. – Anthony Myers Jun 05 '14 at 22:10
  • 1
    It might help to compare to the [sample app reference implementation](https://github.com/railstutorial/sample_app_rails_4) to track down any discrepancies. – mhartl Jun 06 '14 at 01:10
  • Thank you for that link, sir! I'll be referencing that as I go along. – Anthony Myers Jun 06 '14 at 03:23
0

Ok, well it looks like Rails.application.configure do in my config/environments/development.rb file needed to be changed to: SampleApp::Application.configure do. Thanks to this question and answer

Community
  • 1
  • 1
Anthony Myers
  • 393
  • 1
  • 6
  • 18