我遵循第3章的Hartl书,我们从静态页面开始。我们被告知:
$ git checkout -b static-pages
没有问题。然后,我们被告知要执行命令:
$ rails generate controller StaticPages home help --no-test-framework
当我这样做时,我在命令行中得到了这个:
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>'
config/application.rb:31
的第31行是:
config.assets.initialize_on_precompile = false
由于Heroku问题,我添加了。我尝试取出那条线,然后再犯了大约50个错误。
这是我们应该使用的gemfile
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
这是完整的application.rb文件:
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
我可以尝试任何想法吗?
编辑:将这个:config.assets.initialize_on_precompile = false
移到课堂上,如下所示,我收到了很多错误!
以下是前5行错误:/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>'
答案 0 :(得分:3)
晚会,但我(希望!)来了答案:你几乎肯定安装了Rails 4.1和Rails 4.0。您遇到问题的格式对于Rails 4.1是正确的,并且将由Rails 4.1生成。
您可以通过运行gem list --local rails
验证安装了哪些版本。
所以你已经检查并看到你安装了两个版本。现在怎么办?你必须卸载吗?
都能跟得上!生成新项目时,只需指定包含在下划线中的版本号:
rails _4.0.5_ new my_awesome_project
如果没有指定版本号,它将使用最新版本 - 因此您完全有效的Rails 4.1项目在您更换Gemfile时会突然中断。
答案 1 :(得分:1)
如果你把它放在课堂内怎么办?
module SampleApp
class Application < Rails::Application
config.assets.initialize_on_precompile = false
end
end
答案 2 :(得分:1)
答案 3 :(得分:0)
好的,我的config / environments / development.rb文件中的Rails.application.configure do
看起来需要更改为:SampleApp::Application.configure do
。感谢this question and answer