牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

レイルに乗ってみた〜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をhttp://railsofrubyonrails.com/からダウンロードしたサンプルのものと入れ替える。

次は若干端折ってパーシャル使ってみる(_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への移行を試してみる - ザリガニが見ていた...。』さんを参考にごにょごにょしてみた。

  1. 「config/environment.rb」に「require 'gettext/rails'」は書かない!
  2. /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)
とりあえず