【インターン】3日目:業務内容では全くないが、コソーリ日本語訳しつつ勉強なう

本を手に入れられなかったので、Rubyの公式チュートリアルをやってみる。
はてブのコメントを見ていたりすると、結構いいみたいです。
http://guides.rubyonrails.org/getting_started.html#installing-rails
1と2はrailsについての説明なので、ざっと読んで流します。

3 Creating a New Rails Project

blogを作ります。
まず最初のコマンド
よく読んでみると、SQLite用だった・・・ショボーン

C:\Documents and Settings\sea_mountain>rails blog
※長いので省略
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log

間違えました!



DBはMysqlで作りたいので、
rails blog -d mysql

上書きしますかとか言われて怖くなったので・・・すべてnにしました・・・

C:\Documents and Settings\sea_mountain>rails blog -d mysql
※長いので省略
   identical  app/helpers/application_helper.rb
overwrite config/database.yml? (enter "h" for help) [Ynaqdh] h
Y - yes, overwrite
n - no, do not overwrite
a - all, overwrite this and all others
q - quit, abort
d - diff, show the differences between the old and the new
h - help, show this help
overwrite config/database.yml? (enter "h" for help) [Ynaqdh] n
        skip  config/database.yml
※長いので省略
   identical  config/initializers/new_rails_defaults.rb
overwrite config/initializers/session_store.rb? (enter "h" for help) [Ynaqd
        skip  config/initializers/session_store.rb
overwrite config/initializers/cookie_verification_secret.rb? (enter "h" for
) [Ynaqdh] n
        skip  config/initializers/cookie_verification_secret.rb
   identical  config/environment.rb
   identical  config/boot.rb
   identical  script/console
  ※長いので省略

コマンドを実行した場所、この場合はC:\Documents and Settings\sea_mountainにblogというフォルダが出来ていた・・・! 気づいてなかったw
さっきの「上書きしますか?」が気になったので、出来ていたblogフォルダを削除して、もう一度rails blog -d mysqlでとくに変な警告は出ませんでした。

3.3.2 Configuring a MySQL Database

3.3.1はSQLiteの説明なので、3.3.2 Configuring a MySQL Databaseの方を参照します。

development:
adapter: mysqlencoding: utf8database: blog_developmentpool: 5username: rootpassword: socket: /tmp/mysql.sock
If your development computer’s MySQL installation includes a root user with an empty password, this configuration should work for you. Otherwise, change the username and password in the development section as appropriate.

「開発機のMySQLにrootユーザでパスワードがなしで登録されている場合、上の状態で動きます。ユーザ名とパスワードを変えている場合は適宜変更してください。」
ということで、上のpasswordのところに開発機のパスワードを入力しました。
mysql
select user,host,password from mysql.user;
をして、実行結果から、以下のusernameなどを変更したほうがよさそうです。こんな感じにすると、次の段階のrakeコマンドでエラーが出ませんでした。

development:
  adapter: mysql
  encoding: utf8
#  reconnect: false
  database: blog_development
  pool: 5
  username: root 
  password: "パスワードだよ!"
  socket: /var/lib/mysql/mysql.sock
#  host: localhost

3.4 Creating the Database

rakeコマンドを打つ前に、ちゃんと、cd ./blogで作られたフォルダの中に入っておきます。
入ったあとに、

rake db:create

エラーが出ているときは、上のdevelopmentの部分が間違っていると思うので、直します。
socketの部分は、公式サイトに書かれているpathが間違っているようなので、注意です!
実際に、/tmp/mysql.sockを探しても、存在していませんでした。
socket: /tmp/mysql.sock これを  socket: /var/lib/mysql/mysql.sockに直します。
参考サイト(http://fg-180.katamayu.net/archives/2005/08/25/185402)

4 Hello, Rails!

TIP. If you’re on Windows, or your Ruby is set up in some non-standard fashion, you may need to explicitly pass Rails script commands to Ruby: ruby script/generate controller home index.

「windowsの人や、通常の方法でRubyをセットアプしていない人は、
ruby script/generate controller home index
のコマンドを使ってね。」
と書かれているようなのでこちらのコマンドを利用します。

C:\Documents and Settings\sea_mountain\blog>ruby script/generate controller home
 index
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/home
      exists  test/functional/
      create  test/unit/helpers/
      create  app/controllers/home_controller.rb
      create  test/functional/home_controller_test.rb
      create  app/helpers/home_helper.rb
      create  test/unit/helpers/home_helper_test.rb
      create  app/views/home/index.html.erb

railsはテンプレートを作ってくれる。そのうちの1つが
app/views/home/index.html.erb これをエディターで開いて、

<h1>Hello, Rails!</h1>

を追加してみてね」
と書かれているようなので、とりあえず追加。

4.1 Starting up the Web Server

You actually have a functional Rails application already – after running only two commands! To see it, you need to start a web server on your development machine. You can do this by running another command

railsアプリケーションの準備は整いました!あとは2つのコマンドで動きます!開発機で、サーバーを始動し、コマンドを打ってください」
script/server
ですが、windowsでは、

 ruby script/server

にしないと、
「'script' は、内部コマンドまたは外部コマンド、操作可能なプログラムまたはバッチ ファイルとして認識されていません。」
と怒られてしまうので注意。

C:\Documents and Settings\sea_mountain\blog>ruby script/server
=> Booting Mongrel
=> Rails 2.3.8 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server

動いてそうなので、
http://localhost:3000/
にアクセスします。公式サイトに貼ってある画面(railsのアイコンとかが載っている画面)が表示されれば成功です。
「自分の作ったものにアクセスするためには http://localhost:3000/home/index にアクセスしてね」
との事なのでアクセスすると、さっき書いたh1タグの部分が表示されたページが出てきます。やった!

4.2 Setting the Application Home Page

“Welcome Aboard” pageを消して、自分のホームページに置き換えたい場合の方法が載っています。
まず、public/index.htmlを削除します。載っているのは
rm public/index.html
というコマンドですが、こちらはwindowsなので・・・・public/index.htmlを手動で削除します・・・。
削除したあと、
config/routes.rbに記述を追加します。
下のほうは以下のようになっているのですが、

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

一番上に

  map.root :controller => "home"
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

という感じに map.root :controller => "home"を書き足します。
一番下にしてしまうと、その後のアクセスで表示されなくなってしまうので注意です。
http://localhost:3000/ にアクセスすると、何も表示されませんでしたが、もう一度、
ruby script/server をしたところ、さっきまで、http://localhost:3000/home/index
で表示されていた内容が表示されました。
map.root :controller => "home"でhomeディレクトリをrootにするという指定が行われているみたいです。
/views/homeディレクトリにはindex.html.erbがあるのですが、それを参照しています。

6 Creating a Resource

C:\Documents and Settings\sea_mountain\blog>ruby script/generate scaffold Post n
ame:string title:string content:text
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/posts
      exists  app/views/layouts/
      exists  test/functional/
      exists  test/unit/
      exists  test/unit/helpers/
      exists  public/stylesheets/
      create  app/views/posts/index.html.erb
      create  app/views/posts/show.html.erb
      create  app/views/posts/new.html.erb
      create  app/views/posts/edit.html.erb
      create  app/views/layouts/posts.html.erb
      create  public/stylesheets/scaffold.css
      create  app/controllers/posts_controller.rb
      create  test/functional/posts_controller_test.rb
      create  app/helpers/posts_helper.rb
      create  test/unit/helpers/posts_helper_test.rb
       route  map.resources :posts
  dependency  model
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/post.rb
      create    test/unit/post_test.rb
      create    test/fixtures/posts.yml
      create    db/migrate
      create    db/migrate/20100819105109_create_posts.rb

いろいろな説明が続きますが、とりあえず必要そうなコマンド
rake db:migrate
実行です。

C:\Documents and Settings\sea_mountain\blog>rake db:migrate
(in C:/Documents and Settings/sea_mountain/blog)
==  CreatePosts: migrating ====================================================
-- create_table(:posts)
   -> 0.0930s
==  CreatePosts: migrated (0.0930s) ===========================================

とりあえず、
「rake db:migrateするまえに、migrationsはするなよ。」
と書かれていました。

Because you’re working in the development environment by default, this command will apply to the database defined in the development section of your config/database.yml file.

「デフォルトで開発環境で開発しているときに、migrationsを走らせてしまうと、config/database.yml fileの開発部分でDBが定義されてしまうようになってしまうから。」
という訳でいいのでしょうか・・・。

6.2 Adding a Link

「今までに作ったものにアクセスするために、/app/views/home/index.html.erbに<%= link_to "My Blog", posts_path %>を追加で記述してみてー。postsへのリンクを貼るよ」

6.3 Working with Posts in the Browser

http://localhost:3000/にアクセスすると、リンクが増えています。そのリンクをクリックすると、さらに新しいページが!そして、そのページでadd postだかをクリックすると、さらに投稿ページが!
実際に投稿もできる!
なんだこれ!!!!感動した!!!!!!!!!!!!
すごい・・・これはすごい・・・ コマンド打っていただけなのに・・・こんなにちょっとの時間でこれだけのものができるとは・・・
感動しすぎて帰る気+食欲が喪失しそうです・・・

「投稿を追加したり削除したりできるけど、これは、script/generate scaffoldのコマンド一つでできてるのさ」ってなにこれかっこいい・・・

TIP. In development mode (which is what you’re working in by default), Rails reloads your application with every browser request, so there’s no need to stop and restart the web server.

「開発中とか、いわゆる通常の状態だと、railsはブラウザからのリクエストでリロードしているので、サーバーを止めたり再始動させる必要はないよー。」

Congratulations, you’re riding the rails! Now it’s time to see how it all works.

テンション上がってきた!!!
明日もガンバロー!^^ノ

※本来はアイディアの詰めです^^^;;;(明日も