Rails1.2.3でLoginEngineを使う
ライド・オン・Rails Ruby on Railsを徹底攻略
掲載のサンプルが動かなくなっていたので、本日時点での動かし方をまとめてみた。
プラグインの導入
% ruby script/plugin discover % ruby script/plugin install engines % ruby script/plugin install login_engine
engines 1.2.0、login_engine 1.0.2が入った。
% head -1 vendor/plugins/engines/CHANGELOG = 1.2.0 - MASSIVE INTERNAL REFACTORING % head -1 vendor/plugins/login_engine/CHANGELOG = v1.0.2
設定
config/environment.rb
(コメント省いて全文)
RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION require File.join(File.dirname(__FILE__), 'boot') require File.join(RAILS_ROOT, "vendor", "plugins", "engines", "lib", "engines", "deprecated_config_support") Rails::Initializer.run do |config| config.plugins = ["engines", "login_engine"] end module LoginEngine config :salt, "your-salt-here" config :use_email_notification, false config :user_table, "users" end
app/controllers/application.rb
(コメント省いて全文)
require 'login_engine' class ApplicationController < ActionController::Base before_filter :login_required include LoginEngine helper :user model :user session :session_key => '_recipe_session_id' end
app/helpers/application_helper.rb
module ApplicationHelper include LoginEngine end
マイグレーション
マイグレーションファイルを作成してから、rakeを実行する。
% ruby script/generate plugin_migration login_engine % rake db:migrate
ログイン画面作成
app/views/layouts/user.rhtml を作成する。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> <title>LoginTest: <%= controller.action_name %></title> <%= stylesheet_link_tag 'login_engine', :plugin => "login_engine" %> </head> <body> <p style="color: green"><%= flash[:notice] %></p> <%= @content_for_layout %> </body> </html>
動作確認
script/server を起動、もしくは再起動する。
http://localhost:3000/user/login/ にアクセス。