railsでhello world
画面にHelloWorldを表示する
コントローラの作成
script generateで作成
$ ./script/generate controller Hello exists app/controllers/ exists app/helpers/ create app/views/hello exists test/functional/ create test/unit/helpers/ create app/controllers/hello_controller.rb create test/functional/hello_controller_test.rb create app/helpers/hello_helper.rb create test/unit/helpers/hello_helper_test.rb
indexメソッドの追加
とりあえず、メッソドの中身は空で。
$ vim app/controllers/hello_controller.rb
class HelloController < ApplicationController def index end end
コントローラのindexメッソドに対応するビューの作成
$ vim app/views/hello/index.html.erb
Hello World
動かして確認
$ ./script/server
下記、URLにアクセスしたら、
http://192.168.1.24:3000/Hello
こんな感じのメッセージがでた。
We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly.
ターミナル上には、以下のエラーメッセージ
$ ./script/server => Booting WEBrick => Rails 2.3.5 application starting on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2010-04-07 02:24:37] INFO WEBrick 1.3.1 [2010-04-07 02:24:37] INFO ruby 1.8.7 (2010-01-10) [x86_64-linux] [2010-04-07 02:24:37] INFO WEBrick::HTTPServer#start: pid=3023 port=3000 /!\ FAILSAFE /!\ Wed Apr 07 02:24:40 +0900 2010 Status: 500 Internal Server Error no such file to load -- sqlite3 /usr/local/ruby/1.8.7/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' 省略 /usr/local/ruby/1.8.7/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' ./script/server:3 [2010-04-07 02:25:20] INFO going to shutdown ... [2010-04-07 02:25:20] INFO WEBrick::HTTPServer#start done. Exiting
どうやら、sqlite3がないらしい
sqlite3-rubyのインストール
$ sudo gem install sqlite3-ruby Building native extensions. This could take a while... ERROR: Error installing sqlite3-ruby: ERROR: Failed to build gem native extension. /usr/local/ruby/1.8.7/bin/ruby extconf.rb checking for fdatasync() in -lrt... yes checking for sqlite3.h... no *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. 省略
sqlite-develがなく、sqlite3-rubyが入らないみたいなので、sqlite-develをyumでいれる。
$ sudo yum install sqlite-devel $ sudo gem install sqlite3-ruby
改めて確認
$ ./script/server
HelloWorld
表示された