ruby on rails教程 - rails服务器问题

时间:2014-05-18 06:10:39

标签: ruby-on-rails ruby ruby-on-rails-4 railstutorial.org

我刚开始做Michael Hartl的“Ruby on Rails教程”。我用:

创建了一个新项目
rails new first_app

然后他让我改变了Gemfile

source 'https://rubygems.org'

# Use sqlite3 as the database for Active Record
gem 'sqlite3'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.1'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster.
# Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.0.1'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

对此:

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.5'

group :development do
  gem 'sqlite3', '1.3.8'
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 'jbuilder', '1.0.2'

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

现在,当我尝试运行rails server时,我收到此错误:

PS C:\Users\Thomas\rails_projects\first_app> rails server
=> Booting WEBrick
=> Rails 4.0.5 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
C:/Users/Thomas/rails_projects/first_app/config/environments/development.rb:1:in `<top (required)>': undefined method `c
onfigure' for #<FirstApp::Application:0x2a2bae8> (NoMethodError)
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.0.5/lib/active_support/dependencies.rb
:229:in `require'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.0.5/lib/active_support/dependencies.rb
:229:in `block in require'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.0.5/lib/active_support/dependencies.rb
:214:in `load_dependency'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.0.5/lib/active_support/dependencies.rb
:229:in `require'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/engine.rb:591:in `block (2 le
vels) in <class:Engine>'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/engine.rb:590:in `each'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/engine.rb:590:in `block in <c
lass:Engine>'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/initializable.rb:30:in `insta
nce_exec'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/initializable.rb:30:in `run'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/initializable.rb:55:in `block
 in run_initializers'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/tsort.rb:150:in `block in tsort_each'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/tsort.rb:183:in `block (2 levels) in each_strongly_connected_com
ponent'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/tsort.rb:210:in `block (2 levels) in each_strongly_connected_com
ponent_from'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/tsort.rb:219:in `each_strongly_connected_component_from'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/tsort.rb:209:in `block in each_strongly_connected_component_from
'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/initializable.rb:44:in `each'

        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/initializable.rb:44:in `tsort
_each_child'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/tsort.rb:203:in `each_strongly_connected_component_from'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/tsort.rb:182:in `block in each_strongly_connected_component'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/tsort.rb:180:in `each'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/tsort.rb:180:in `each_strongly_connected_component'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/tsort.rb:148:in `tsort_each'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/initializable.rb:54:in `run_i
nitializers'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/application.rb:215:in `initia
lize!'
        from C:/Users/Thomas/rails_projects/first_app/config/environment.rb:5:in `<top (required)>'
        from C:/Users/Thomas/rails_projects/first_app/config.ru:3:in `require'
        from C:/Users/Thomas/rails_projects/first_app/config.ru:3:in `block in <main>'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
        from C:/Users/Thomas/rails_projects/first_app/config.ru:in `new'
        from C:/Users/Thomas/rails_projects/first_app/config.ru:in `<main>'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'

        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_op
tions_from_config'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/commands/server.rb:48:in `app
'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/commands/server.rb:75:in `sta
rt'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/commands.rb:76:in `block in <
top (required)>'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/commands.rb:71:in `tap'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.5/lib/rails/commands.rb:71:in `<top (requ
ired)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

我知道更改Gemfile时会发生错误,因为它在我更改之前有效。我想尽可能地跟随迈克尔。唯一不同的是我正在使用Ruby 1.9.3并且他正在使用Ruby 2.0,尽管在书中他说如果不使用2只是改变我在Gemfile中所做的那样。

5 个答案:

答案 0 :(得分:1)

您需要更改 app / config / initializers / development.rb

Rails.application.configure do

YourApp::Application.configure do

但你会看到错误:

内部服务器错误您必须在应用的配置中设置config.secret_key_base

要解决此问题,您应该生成一个rnd密钥

$ rake secret

然后创建文件 config / initializers / secret_token.rb 并将其放入此文件

YourApp::Application.config.secret_key_base = 'YOUR_SECRET_KEY'

其中YOUR_SECRET_KEY是生成的,一切正常。

原因是因为存储密钥的新约定(从Rails 4.0升级到Rails 4.1 docs

P.S。注意文件 config / secrets.yml

答案 1 :(得分:1)

你在Gemfile的开头声明了ruby'2.0.0',但你使用的是1.9.3,所以在尝试检测它时会失败。改为红宝石'1.9.3'

此外,您可能需要运行bundle才能安装gem。

答案 2 :(得分:0)

您是否尝试过彻底清除红宝石线?我的gemfile中通常没有这一行

答案 3 :(得分:0)

这是您收到的错误:

C:/Users/Thomas/rails_projects/first_app/config/environments/development.rb:1:in `<top (required)>': undefined method `configure' for #<FirstApp::Application:0x2a2bae8>

这基本上意味着您在文件line 1

/config/environments/development.rb上出错

-

您提到更改Gemfile时错误消失。我怀疑问题是你没有在你的系统上安装Ruby 1.9.3,令Rails&amp;混乱阻止它加载

我的建议是(重新)在您的系统上安装Ruby 1.9.3Ruby 2.0.0

答案 4 :(得分:0)

我有同样的问题。

通过

检查您的导轨版本
  

rails -v

然后在你的应用程序的Gemfile中更改确保 你有按照版本的rails 在我的情况下,你的计算机就是:

  

gem&#39; rails&#39;,&#39; 4.1.1&#39;

现在终于运行了

  

捆绑更新   捆绑安装

现在您应该可以运行您的服务器了。