Rails3.2 でコントローラから Hello world! を表示する
前回 のつづき。
Ruby on Rails 3 アプリケーションプログラミング
- 作者: 山田祥寛
- 出版社/メーカー: 技術評論社
- 発売日: 2011/05/12
- メディア: 大型本
- 購入: 24人 クリック: 634回
- この商品を含むブログ (35件) を見る
まずは Rails プロジェクトの作成から。
$ mkdir ~/rails; cd ~/rails
$ rails new hellorails
次に rails generate コマンドを使ってコントローラクラスを作成する。
$ rails generate controller hello
create app/controllers/hello_controller.rb
invoke erb
create app/views/hello
invoke test_unit
create test/functional/hello_controller_test.rb
invoke helper
create app/helpers/hello_helper.rb
invoke test_unit
create test/unit/helpers/hello_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/hello.js.coffee
invoke scss
create app/assets/stylesheets/hello.css.scss
ずらっとファイルが生成される。
ちなみに、rails generate で自動生成したファイルは、rails destroy コマンドでまとめて削除することができる。
$ rails destroy controller hello
Hello world! を表示するために、コントローラクラスファイルの hello_controller.rb を編集する。
class HelloController < ApplicationController def index render :text => 'Hello world!' end end
日本語も表示したいぜって場合には、ファイルの行頭に coding: utf-8
を追記。
# coding: utf-8 class HelloController < ApplicationController def index render :text => 'こんにちは、世界!' end end
さらに、サーバがリクエストを受けた際に作成したコントローラを呼び出すよう、ルーティングの設定を行う。
$ vim config/routes.rb
# This is a legacy wild controller route that's not recommended for RESTful applications. # Note: This route will make all actions in every controller accessible via GET requests. match ':controller(/:action(/:id))(.:format)'
最後に、サーバを起動して、http://localhost:3000/hello/index にアクセスし、Hello world!
の文字が表示されていることを確認する。
$ rails server
ついつい facebook とか見てしまうので /etc/hosts でアクセス制限かけてみた
ついつい facebook とか見てしまうので /etc/hosts に閲覧したくないウェブサイトのホスト名と IP (127.0.0.1) を書いてアクセス制限をかけてみた。
手順
sudo vim /etc/hosts
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost # このへんを追記 127.0.0.1 www.youtube.com www 127.0.0.1 www.facebook.com www 127.0.0.1 twitter.com 127.0.0.1 b.hatena.ne.jp
Mac でデフォルトのテキストエディタを設定する方法
最近 Mac のセットアップ関連が多いです。
Homebrew をインストールした
MacPortsより使いやすい!?パッケージ管理システムHomebrewの使い方 | Macとかの雑記帳 によると、
MacPortsと比べて依存関係でインストールされるソフトが少ないためか、パッケージ管理にHomebrewを使う人が増えているそうです。
MacPortsは、Macに最初から入っているソフトを無視してパッケージが依存するソフトを新規でインストールするという性質を持っていますが、Homebrewは極力Macに入っているものを使うように作られています。
このため、パッケージ導入時のシステムへの負担や、インストールにかかる時間が比較的少なくて済みます。
とのことなので、試しにMacPortsの代わりにHomebrewを入れてみることにしました。
参考
手順
$ ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go) ==> This script will install: /usr/local/bin/brew /usr/local/Library/... /usr/local/share/man/man1/brew.1 ==> The following directories will be made group writable: /usr/local/. ==> The following directories will have their group set to admin: /usr/local/. Press enter to continue ==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/. Password: ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/. ==> Downloading and Installing Homebrew... remote: Counting objects: 82165, done. remote: Compressing objects: 100% (38608/38608), done. remote: Total 82165 (delta 56664), reused 65899 (delta 42732) Receiving objects: 100% (82165/82165), 11.58 MiB | 2.16 MiB/s, done. Resolving deltas: 100% (56664/56664), done. From https://github.com/mxcl/homebrew * [new branch] master -> origin/master HEAD is now at 9b40a73 flac2mp3: fix hash ==> Installation successful! You should run `brew doctor' *before* you install anything. Now type: brew help
brew doctor でインストールの状況を確認して、とのこと。
$ brew doctor Warning: You have Macports or Fink installed. This can cause trouble. You don't have to uninstall them, but you may like to try temporarily moving them away, eg. sudo mv /opt/local ~/macports
MacPorts がインストールされていると問題の原因になるらしいので、提案通り /opt/local 以下を mv しておくことに。
$ ls /opt/local Library bin etc include lib libexec man sbin share var $ sudo mv /opt/local ~/macports Password: $ brew doctor Your system is raring to brew.
以上でインストール完了。
TextMate がオープンソース化されたのでインストールした
Mac OX 向けテキストエディタの TextMate がオープンソース化された。
TextMate の存在を初めて知ったのは YouTube で、Google のエンジニアが Google App Engine の使い方をデモを交えながら軽快に解説するみたいな動画があって、その動画のデモで使われていたのが TextMate だった。直感的にいいなと思って調べたら 50 ドルくらいして、ちょっと高いなあと思いつつ 30 日のトライアルを試した後に使うのをやめてしまった。その後は Vim と CotEditor を使っていた。
それが今回オープンソース化され、GitHub にソースが上がっているとのことなので、ダウンロードしてインストールしてみた。
前提
- OS : Mac OS X 10.8 Mountain Lion
手順
まずは GitHub からソースをダウンロードする。
$ git clone https://github.com/textmate/textmate.git
Git がインストールされていない場合はインストール。
sudo port install git-core
参考
ちなみに、MacPorts を使うには The MacPorts Project -- Download & Installation のダウンロードと XCode の Command Line Tools のダウンロードが必要。Command Line Tools のダウンロードは こちら から。
参考
で、textmate を落としてきたところで、GitHub の textmate の README を見ると、
To build the source the following must first be installed on your system:
ninja — build system similar to make
ragel — state machine compiler
boost — portable C++ source libraries
multimarkdown — marked-up plain text compiler
mercurial — distributed SCM system
ninja, ragel boost, multimarkdown, mercurial を最初にインストールしてねと書いてある。
$ sudo port install ninja ragel boost multimarkdown mercurial
そんなわけで必要なモジュールがそろったら、TextMate をビルドしてインストール。
cd textmate git submodule update --init ./configure && ninja
ちなみに 10.7 Lion の場合は、追加で必要なモジュールがあるのでインストールしておく。
sudo port install proctools
インストールが正常に終われば TextMate のウインドウが自動で立ち上がってくる。
あと、ビルド後のファイルが ~/build に残っているので、これを他のアプリケーションと同様、/Application フォルダに移動しておく。
cp -r ~/build/TextMate/Applications/TextMate/TextMate.app /Applications/
Solr vs ElasticSearch の話
RSS 購読している Sematext - Search & Big Data Analytics - Solr ElasticSearch Hadoop HBase Lucene Experts のブログ記事 Solr vs. ElasticSearch: Part 1 – Overview « Sematext Blog を読んだ。
ElasticSearch の名前は少し前から聞くようになって、こないだの #pyfes でも発表があったりで興味をもった。
ざっくりとした印象としては、ElasticSearch の方が Solr と比較して、より少ない設定でドキュメントのフィールド型などを柔軟に吸収してくれるのがよさそう。慣れないと難しそうだけど。一方 Solr は SolrCloud の Availability のアーキテクチャ(ここは ElasticSearch の Zen Discovery の説明がよく理解できなかったが)やクエリとレスポンス周りの対応の広さが ElasticSearch と比較してより使いやすくなっているように感じた。
いろんなソフトウェアを触ってみるのは大事だと思うので、ElasticSearch も時間を作って試してみたい。もちろん Solr もまだまだなんだけど。
Git はじめました(ステージの取り消し、ファイルの削除と移動、ログの閲覧、コミットのやり直し)
ステージされたファイルの取り消し
git status で確認してみると以下のような状況になっている。これは add されている hello.py をさらに手元で修正しているためである。いったんステージしたファイルを元に戻すには、git reset HEAD
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: hello.py # # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: hello.py ... $ git reset HEAD hello.py Unstaged changes after reset: M hello.py $ git status # On branch master # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: hello.py ... no changes added to commit (use "git add" and/or "git commit -a") ** ファイルの削除 ファイルを Git から削除するには、git rm <file> でステージされたファイルを削除し、コミットする。このとき、手元のファイルとステージされたファイルに差分があるとエラーになるので、コミットするかもしくは手元のファイルを削除してから実行する >|| $ rm hello.py $ git rm hello.py rm 'hello.py' $ git commit -m 'delete hello.py' [master 763004b] delete hello.py 1 files changed, 0 insertions(+), 1 deletions(-) delete mode 100644 hello.py
ファイルの移動
Git には mv コマンドがあり、リポジトリのファイル名を変更できる。
$ git mv README README.txt oiwatatsuya01@ubuntu101:~/workspace/src/sampleproject$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: README -> README.txt ...
ちなみに、この操作は次のコマンドを実行しているのと同じ意味となる。
$ mv README.txt README $ git rm README.txt $ git add README
ログの閲覧
ログの閲覧は git log コマンドを使う。
$ git log commit 763004b4e1b0d85633a1d03cdafcb04c3339cf16 Author: Tatsuya Oiwa <example@gmail.com> delete hello.py ... commit 78f74b581a1568da78c9adaa959f434bc27ae6aa Author: Tatsuya Oiwa <example@gmail.com> initial project version
format オプションは強力で、独自のログ出力フォーマットを指定することができる。
$ git log --pretty=format:"%h - %an, %ar : %s" 2ad5293 - Tatsuya Oiwa, 10 minutes ago : delete hello.py file e77894a - Tatsuya Oiwa, 30 minutes ago : second commit 78f74b5 - Tatsuya Oiwa, 48 minutes ago : initial project version
他にもさまざまなオプションが用意されている。詳しくは公式ドキュメントで。
コミットのやり直し
「コミットを早まりすぎて追加すべきファイルを忘れてしまった」ときや「コミットメッセージが変になってしまった」ときなどは、--amend オプションでコミットをやり直せる。
$ git commit -m 'Move README' $ echo "print 'Hello'" > hello.py $ git add hello.py $ git commit --amend -m 'Move README, add hello.py' [master c3b9852] Move README, add hello.py 2 files changed, 1 insertions(+), 0 deletions(-) rename README => README.txt (100%) create mode 100644 hello.py $ git log commit c3b985260df108c01a49a936c012c9c4c2d9bcf0 Author: Tatsuya Oiwa <example@gmail.com> Move README, add hello.py commit 763004b4e1b0d85633a1d03cdafcb04c3339cf16 Author: Tatsuya Oiwa <example@gmail.com> delete hello.py
Git は基本的にすべての操作を「やり直せる」ようになっている。もし期待と違った動作をしてしまった場合でも落ち着いて対応方法を調べるようにしたい。