2010年度作業ログ
<%= current_user.login %> さん、研究室配属システムへようこそ!
<%= 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 ---------- %> ...(中略)...<%= link_to 'Signup >>', new_user_path %>
==== ユーザー登録ページから、ログインページへのリンク作成 app/views/users/new.html.erb を以下のように編集 <%# ---------- app/views/users/new.html.erb ---------- %> ...(中略)...<%= link_to '<< Cancel', new_session_path %>
==== いつでもログアウトできるように設定 ログアウトのリンクをレイアウトファイルに設定。 ファイル名を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 ---------- %><%= link_to_if logged_in?, 'Logout >>', session_path, :method => :delete do end %>
<%= flash[:notice] %>
<%= yield %> === メール認証の設定 ==== 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/12171410052011/11/28 堺 正太朗 作成