Rails教程2.2:Puma Server

时间:2016-08-30 22:50:30

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

我正在尝试从Rails教程书中学习Rails,得到了lubuntu所以我可以尝试获得最佳体验。现在我坚持做一些应该简单直接的事情。

我们正在创建一个小小的“玩具应用程序”,这是一个非常基本的推特类似的东西,仍然在最开始,我们使用:

$ rails generate scaffold User name:string email:string

如果我没有弄错,要生成用户表,然后:

$ rails db:migrate

我想要更新一个预先存在的表格结构(不确定,但是后面会说明所有内容都会被解释)。

最后我们应该运行这个:

 $ rails server -b $IP -p $PORT

在一个不同的标签中,我做了并得到了这个错误:

~/workspace/toy_app$ rails server -b $IP -p $PORT
=> Booting Puma
=> Rails 5.0.0.1 application starting in development on http://-p:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.4.0 (ruby 2.3.1-p112), codename: Owl Bowl Brawl
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://-p:3000
Exiting
/var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:255:in `initialize': getaddrinfo: Name or service not known (SocketError)
    from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:255:in `new'
    from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:255:in `add_tcp_listener'
    from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:102:in `block in parse'
    from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:85:in `each'
    from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:85:in `parse'
    from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/runner.rb:129:in `load_and_bind'
    from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/single.rb:84:in `run'
    from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/launcher.rb:172:in `run'
    from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/rack/handler/puma.rb:51:in `run'
    from /var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/server.rb:296:in `start'
    from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/server.rb:79:in `start'
    from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:90:in `block in server'
    from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:85:in `tap'
    from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:85:in `server'
    from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
    from /home/zero/workspace/toy_app/bin/rails:9:in `require'
    from /home/zero/workspace/toy_app/bin/rails:9:in `<top (required)>'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `load'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `call'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/client/command.rb:7:in `call'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/client.rb:30:in `run'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/bin/spring:49:in `<top (required)>'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `load'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `<top (required)>'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /home/zero/workspace/toy_app/bin/spring:13:in `<top (required)>'
    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'

有什么损坏的吗?我做错了什么吗?我去检查我记得看到puma(在app文件夹中)的文件,它位于gem文件中:

gem 'puma',         '3.4.0'

和puma.rb文件(拿出评论):

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count

port        ENV.fetch("PORT") { 3000 }

environment ENV.fetch("RAILS_ENV") { "development" }

plugin :tmp_restart

我打算把所有提到的文件放进去,但是有很多代码,如果有任何有用的东西我会发布它,我不想用可能无用的代码发送垃圾邮件。

3 个答案:

答案 0 :(得分:1)

您的环境中未定义

$IP,因此rails server正在尝试使用-p作为IP地址。试试这个:

rails server -b ${IP:-127.0.0.1} -p ${PORT:-3000}

或者,在运行export

之前,使用rails server -b $IP -p $PORT设置这些环境变量
export IP=127.0.0.1
export PORT=3000

或者如果你对Rails的默认设置没问题,那就忘记这一切并运行rails server。 :-)

答案 1 :(得分:1)

如果您在本地运行,则应该能够使用不带参数的rails server。如果您使用的是Hartl教程,则假定您使用的是基于Web的IDE,需要额外的参数。

答案 2 :(得分:0)

如果您使用cloud based IDE,则需要在particular IP & PORT收听。例如,C9,您需要收听0.0.0.0 and 8080。所以他们设置了environment variables, $IP & $PORT.

如果你执行,

echo $IP  ###gives 0.0.0.0
echo $PORT ###gives 8080

rails s -b $IP -p $PORT  ##Rails x.x.x application starting in development on http://0.0.0.0:8080

-b选项将Rails绑定到指定的IP,默认情况下它是localhost。

如果您在本地计算机上,请运行

rails s ### Rails x.x.x application starting in development on http://localhost:3000