2008-09-08
レイルに乗ってみた〜Rails of Ruby on Rails その02「GetTextで躓いた(つдT)」
書籍『Rails of Ruby on Rails ~Case of LOCUSANDWONDERS.COM~』で勉強してみた(p062〜)
レイアウトを変更する
app/views/layouts/entries.html.erbに変更・追加
<title>Blog</title> : <div id="container"> <ul id="navi"> <li id="naviBlog"><%= link_to 'Blog', entries_path %></li> </ul> <h1><%= image_tag 'blog_header.jpg' %></h1> <%= content_tag(:div, flash[:notice], :id => 'notice') if flash[:notice] %> <div id="content"> : </div> </div>
public/ディレクトリにあるimagesとstylesheetsをRails of Ruby on Rails サポートサイトからダウンロードしたサンプルのものと入れ替える。
次は若干端折ってパーシャル使ってみる(_entry.html.erb、_form.html.erb)
パーシャルを利用してDRYに実装
app/views/entries/_entry.html.erbファイル作成して書籍P68のとおり書くと、なぜかブラウザで「編集」「削除」あたりの表示がおかしくなる(『"operation"> 編集 削除』と表示される)
<dd class => "operation"%> <%= link_to '編集', edit_entry_path(entry) %> <%= link_to '削除', entry, :confirm => '本当によろしいですか?', :method => :delete %>
の部分を
<dd> <%= link_to '編集', edit_entry_path(entry), :class => "operation"%> <%= link_to '削除', entry, :confirm => '本当によろしいですか?', :method => :delete, :class => "operation"%>
にしたら書籍と同じ表示になった。
同じディレクトリにある「index.html.erb」と「show.html.erb」も書籍にそって書き直す。
それから同じように「_form.html.erb」はファイルを作って、「new.html.erb」「edit.html.erb」それぞれ書き直す。
バリデーションを使ってフォームの入力内容を検証する
「app/models/entry.rb」
class Entry < ActiveRecord::Base validates_presence_of :title, :content end
と一行追加して、ブラウザで確認。新規作成時にタイトルと記事に何もいれないとエラーでる。英語で。
Ruby-GetText-Packageをインストール
[訂正]Macportsでrb-gettextをインストール追加
日本語対応にするためでRuby-GetText-Packageを、Macportsもしくはgemでインストール。
ターミナルから
Macportsでインストールの場合
sudo port install rb-gettext
gemでインストールの場合
sudo gem install gettext
[重要!!]
ここから書籍のままやるとエラーになるので『Rails2.0から2.1への移行を試してみる - ザリガニが見ていた...。』さんを参考にごにょごにょしてみた。
- 「config/environment.rb」に「require 'gettext/rails'」は書かない!
- /config/initializers/gettext.rbを新規に作って以下内容を書く。
require 'gettext/rails' module ActionView class Base delegate :file_exists?, :to => :finder unless respond_to?(:file_exists?) end end
以上、書籍外情報でした。
書籍に戻って
「app/controllers/application.rb」(helper :all〜の下あたり)に
init_gettext "locus"
を追加。サーバを再起動して確認(script/server )
(〜p075)
とりあえず
- 8 http://reader.livedoor.com/reader/
- 7 http://fastladder.com/reader/
- 3 http://d.hatena.ne.jp/keyword/Carbon Emacs
- 3 http://www.google.com/reader/view/
- 2 http://a.hatena.ne.jp/akhi/
- 2 http://d.hatena.ne.jp/keyword/rails
- 2 http://d.hatena.ne.jp/yuum3/
- 2 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja:official&q=python+変数展開+文字列&btnG=検索&lr=lang_ja
- 1 http://209.85.175.104/search?q=cache:lxp_LYClE3QJ:d.hatena.ne.jp/CortYuming/20080708/p4+python+三項演算子&hl=ja&ct=clnk&cd=4&gl=jp&lr=lang_ja&client=firefox
- 1 http://b.hatena.ne.jp/entry/http://d.hatena.ne.jp/next49/20080807/p1