[PRESS 0007] Sample Application (BLOG) の作成 [2nd]
RAILS PRESS » Sample Application (BLOG) の作成 [1st]の続きを..
LoginGeneratorでログインの仕組みを作る
RailsではLoginGeneratorというものを使って簡単にログインの仕組みを作ることができます。まずgemでlogin_generatorをインストールします。
-
gem install login_generator
次にログイン管理用コントローラを作成しましょう。ここではaccount_controllerという名前のコントローラを作成します。
-
./script/generate login account
次にApplicationController(RAILS_ROOT/app/controllers/application_controller.rb)を以下のように編集してください。
-
# Filters added to this controller will be run for all controllers in the application.
-
# Likewise, all the methods added will be available for all controllers.
-
-
require_dependency "login_system"
-
class ApplicationController <ActionController::Base
-
include LoginSystem
-
model :user
-
end
既にuserテーブルを以下のように定義していると思いますが、LoginGeneratorではloginとpasswordというカラムを必要としますので、違う名前のカラムを作ってしまっている場合はもう一度DBを作成しなおしてください。
-
create_table :users do |t|
-
t.column :login, :string
-
t.column :password, :string
-
t.column :name, :string
-
t.column :mail, :string
-
t.column :profile, :text
-
end
あとはentrirs_controllerで以下の一行を加えてください。これでentries_controllerでshowとlist以外のアクションにログインが要求されます。
-
before_filter :login_required, :except => [:show, :list]
場所は
-
class EntriesController <ApplicationController
直下でOKです。
では早速WEBrickを立ち上げて(./script/serverを実行)、http://localhost:3000/entries/newにアクセスしてみましょう。http://localhost:3000/account/loginにリダイレクトされ、ログインが要求されるはずです。サインアップはhttp://localhost:3000/account/signupにアクセスしてください。
同様にusers_controllerでも
-
before_filter :login_required, :except => [:show, :list]
と記述しておいてください。
次はエントリー投稿&編集についてです。
いつになるでしょう?
About this entry
You’re currently reading “ [PRESS 0007] Sample Application (BLOG) の作成 [2nd] ,” an entry on RAILS PRESS
- Published:
- 12.23.06 / 4am
- Category:
- practice
No comments
Jump to comment form | comments rss [?] | trackback uri [?]