日本語化
次に、テスト用の画面を日本語化してみます。
これの付録Fに「日本語を扱うための注意事項」という項目があるので、これを参考に。(言い忘れたというか言うまでもないけど、この本は目茶苦茶参考になってます)
# environment.rbの先頭に追加 $KCODE= "UTF8"
app/controllers/application.rbを変更
require 'login_engine' class ApplicationController < ActionController::Base include LoginEngine helper :user model :user before_filter :set_charset private def set_charset headers["Content-Type"] = "text/html; charset=UTF-8" end end
class InitialSchema < ActiveRecord::Migration def self.up create_table LoginEngine.config(:user_table), :force => true, :options => 'CHARACTER SET utf8' do |t| ....
テスト用のビュー(app/views/test_login/index.rhtml)を以下のように日本語化。
<h1>こんにちは</h1> <% if user? %> <p>こんにちは <%= current_user.lastname %>さん</p> <% else %> <p>こんにちはゲストさん</p> <% end %> <ul> <% unless user? %> <li><%= link_to 'login', :controller => 'user', :action => 'login' %> <% end %> <li><%= link_to "to secret page", :action=>'secret' %> <% if user? %> <li><%= link_to 'edit user information', :controller => 'user', :action => 'edit' %> <% end %> <li><%= link_to 'logout', :controller => 'user', :action => 'logout' %> </ul>
そして、データベースを初期化してから、script/serverで実行し、日本語の名前を登録したら無事に日本語の画面が表示されました。