2010年度作業ログ

== 初期設定 labo 作成 $ rails -d sqlite3 labo $ cd labo データベース作成 $ sqlite3 labo.sqlite3 雛形作成 $ ruby script/generate scaffold Labo first:integer second:integer third:integer fourth:integer fifth:integer # 雛形消す場合は ruby script/destroy scaffold Labo でOK migration を有効にする $ rake db:migrate サーバーの起動 $ ./script server ブラウザで確認 http://localhost:3000/labos # なぜか複数形 タイムゾーンの変更 /lobo/config/environment.rb の中の config.time_zone = 'UTC' をコメントアウトすると LT になる == ユーザ認証のためのプラグインのインストールと設定 === インストール $ ruby script/plugin install git://github.com/technoweenie/restful-authentication.git === 認証を行うモデルとコントローラを定義 $ script/generate authenticated user sessions --include-activation --include-activation はメールによる認証を行うために必要 === マイグレーションを適用する $ rake db:migrate === application.rb の記述更新 controllers/application.rb に include AuthenticatedSystem を以下のように追加 # ---------- app/controllers/application.rb ---------- class ApplicationController < ActionController::Base include AuthenticatedSystem helper :all # include all helpers, all the time ...(中略)... === labos_controller.rb の記述変更 controllers/labos_controller.rb に include AuthenticatedSystem を以下の ように追加 # ---------- app/controllers/todos_controller.rb ---------- class TodosController < ApplicationController before_filter :login_required ...(中略)... === プロジェクトのルートページの作成 config/routes.rbの下の方にコメントアウトされている「map.root :controller => "welcome"」を以下のように有効にした。 # ---------- config/routes.rb ---------- ActionController::Routing::Routes.draw do |map| map.resources :users map.resource :session map.resources :todos ...(中略)... # You can have the root of your site routed with map.root -- just remember to delete public/index.html. map.root :controller => "welcome" # See how all your routes lay out with "rake routes" # Install the default routes as the lowest priority. map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' end welcome コントローラーの作成 $ script/generate controller welcome welcomeコントローラーにも、before_filter :login_requiredを以下のように追記 # ---------- app/controllers/welcome_controller.rb ---------- class WelcomeController < ApplicationController before_filter :login_required end app/views/welcome/index.html.erbを作って、以下のように編集 <%# ---------- app/views/welcome/index.html.erb ---------- %> <p><%= current_user.login %> さん、研究室配属システムへようこそ!</p> <%= link_to '新規作成', labos_path %> http://localhost:3000でwelcomeページにアクセスするために public/index.html の名前を変更 $ mv public/index.html public/index_bk.html === ログイン,ログアウト,ユーザー登録のためのリンク作成 ==== ログインページから,ユーザー登録ページへのリンク作成 app/views/sessions/new.html.erb を以下のように編集 <%# ---------- app/views/sessions/new.html.erb ---------- %> ...(中略)... <p><%= link_to 'Signup >>', new_user_path %></p> ==== ユーザー登録ページから、ログインページへのリンク作成 app/views/users/new.html.erb を以下のように編集 <%# ---------- app/views/users/new.html.erb ---------- %> ...(中略)... <p><%= link_to '<< Cancel', new_session_path %></p> ==== いつでもログアウトできるように設定 ログアウトのリンクをレイアウトファイルに設定。 ファイル名をlabos.html.erbからapplication.html.erbに変更して、すべてのコ ントローラーに共通のレイア ウトにする. $ mv app/views/layouts/labos.html.erb app/views/layouts/application.html.erb 以下のように app/views/layouts/application.html.erb を編集する <%# ---------- app/views/layouts/application.html.erb ---------- %> <!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><%= controller.controller_name %>: <%= controller.action_name %></title> <%= stylesheet_link_tag 'scaffold' %> </head> <body> <%# ---------- ログアウトのリンクを追記(add logout link) ---------- %> <p align="right"> <%= link_to_if logged_in?, 'Logout >>', session_path, :method => :delete do end %> </p> <p style="color: green"><%= flash[:notice] %></p> <%= yield %> </body> </html> === メール認証の設定 ==== config/environment.rbの設定 Userモデルを監視するuser_observerを有効にする. config/environment.rb を以下のように編集する. # ---------- config/environment.rb ---------- ...(中略)... Rails::Initializer.run do |config| ...(中略)... config.active_record.observers = :user_observer end ==== config/routes.rbの設定 メール認証(アクティベーション)用のルートを追加した. config/routes.rb を以下のように編集 # ---------- config/routes.rb ---------- ActionController::Routing::Routes.draw do |map| map.resources :users map.resource :session map.resources :todos map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate' ...(中略)... # You can have the root of your site routed with map.root -- just remember to delete public/index.html. map.root :controller => "welcome" # See how all your routes lay out with "rake routes" # Install the default routes as the lowest priority. map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' end ==== lib/smtp_tls.rb による拡張 Railsプロジェクトのlibフォルダ以下に smtp_tls.rb として以下の内容で保存する # ---------- lib/smtp_tls.rb ---------- require "openssl" require "net/smtp" Net::SMTP.class_eval do private def do_start(helodomain, user, secret, authtype) raise IOError, 'SMTP session already started' if @started check_auth_args user, secret, authtype if user or secret sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) } @socket = Net::InternetMessageIO.new(sock) @socket.read_timeout = 60 #@read_timeout check_response(critical { recv_response() }) do_helo(helodomain) if starttls raise 'openssl library not installed' unless defined?(OpenSSL) ssl = OpenSSL::SSL::SSLSocket.new(sock) ssl.sync_close = true ssl.connect @socket = Net::InternetMessageIO.new(ssl) @socket.read_timeout = 60 #@read_timeout do_helo(helodomain) end authenticate user, secret, authtype if user @started = true ensure unless @started # authentication failed, cancel connection. @socket.close if not @started and @socket and not @socket.closed? @socket = nil end end def do_helo(helodomain) begin if @esmtp ehlo helodomain else helo helodomain end rescue Net::ProtocolError if @esmtp @esmtp = false @error_occured = false retry end raise end end def starttls getok('STARTTLS') rescue return false return true end def quit begin getok('QUIT') rescue EOFError, OpenSSL::SSL::SSLError end end end ==== config/initializers/mail.rbの設定 gmail に送る場合は以下のようにする config/initializers/mail.rbファイルを新規作成して,以下のように編集. # ---------- config/initializers/mail.rb ---------- require "smtp_tls" ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.smtp_settings = { :address => "smtp.gmail.com", :port => 587, :domain => "なんちゃらかんちゃら", :authentication => :login, :user_name => "haizoku.ep", :password => "パスワード" } ==== app/models/user_mailer.rbの設定 以下のように編集. # ---------- app/models/user_mailer.rb ---------- class UserMailer < ActionMailer::Base def signup_notification(user) setup_email(user) @subject += 'Please activate your new account' @body[:url] = "http://localhost:3000/activate/#{user.activation_code}" end def activation(user) setup_email(user) @subject += 'Your account has been activated!' @body[:url] = "http://localhost:3000/" end protected def setup_email(user) @recipients = "#{user.email}" @from = "haizoku.ep@gmail.com" @subject = "[研究室配属システム] " @sent_on = Time.now @body[:user] = user end end === メール認証の動作確認 $ rake db:migrate:reset === 参照ページ http://d.hatena.ne.jp/zariganitosh/20080726/1217141005
2011/11/28 堺 正太朗 作成