2011 年度作業ログ

2011/12/02 以下はユーザー認証ページ導入したときのログです. ====前準備 $ rails new auth Gemfile に以下を追加 gem 'therubyracer' gem 'bcrypt-ruby', :require => 'bcrypt' $ bundle install ====ユーザー情報登録 =====Userコントローラー・モデル作成 UsersController を作成し,new アクションを作る $ rails g controller users new パスワードは決して素のテキストで保存するべきではない その代わりにハッシュ値とソルトを保存する $ rails g model user email:string password_hash:string password_salt:string モデルを作成したら、データベースをmigrateしてusersテーブルを作成する $ rake db:migrate =====new・controllerメソッドの作成 /app/controllers/users_controller.rb に以下のように編集 class UsersController < ApplicationController def new @user = User.new end def create @user = User.new(params[:user]) if @user.save redirect_to root_url, :notice => "Signed up!" else render "new" end end end =====newテンプレートを作る /app/views/users/new.html.erb を以下のように編集する. <h1>Sign Up</h1> <%= form_for @user do |f| %> <% if @user.errors.any?%> <div class="error_messages"> <h2>Form is invalid</h2> <ul> <% for message in @user.errors.full_messages %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <p> <%= f.label :email %><br /> <%= f.text_field :email %> </p> <p> <%= f.label :password %><br /> <%= f.password_field :password %> </p> <p> <%= f.label :password_confirmation %> <%= f.password_field :password_confirmation %> </p> <p class="button"><%= f.submit %></p> <% end %> =====ルート変更 /config/routes.rb を以下のように編集 Auth::Application.routes.draw do get "sign_up" => "users#new", :as => "sign_up" root :to => "users#new" resources :users end =====User 登録項目の定義 /app/models/user.rb を以下のように編集 class User < ActiveRecord::Base attr_accessible :email, :password, :password_confirmation attr_accessor :password before_save :encrypt_password validates_confirmation_of :password validates_presence_of :password, :on => :create validates_presence_of :email validates_uniqueness_of :email def encrypt_password if password.present? self.password_salt = BCrypt::Engine.generate_salt self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) end end end =====ブラウザで入力/登録確認 ここまでで登録可能な状態になったので $ sudo /opt/ruby/bin/rails s -p http://haizoku.ep.sci.hokudai.ac.jp/sign_up で登録 $ rails dbconsole sqlite> .mode column sqlite> .header on sqlite> SELECT * FROM users; で確認 ====ログイン機能 $ rails g controller sessions new =====ログイン用のフォームを作成 /app/views/sessions/new.html.erb を以下のように編集 <h1>Log in</h1> <%= form_tag sessions_path do %> <p> <%= label_tag :email %><br /> <%= text_field_tag :email, params[:email] %> </p> <p> <%= label_tag :password %><br /> <%= password_field_tag :password %> </p> <p class="button"><%= submit_tag %></p> <% end %> =====ルート変更 /config/routes.rb を以下のように編集 Auth::Application.routes.draw do get "log_in" => "sessions#new", :as => "log_in" get "sign_up" => "users#new", :as => "sign_up" root :to => "users#new" resources :users resources :sessions end =====/app/controllers/sessions_controller.rb を編集 class SessionsController < ApplicationController def new end def create user = User.authenticate(params[:email], params[:password]) if user session[:user_id] = user.id redirect_to root_url, :notice => "Logged in!" else flash.now.alert = "Invalid email or password" render "new" end end end =====User.authenticateメソッドの作成 /app/models/user.rb に以下を追記 def self.authenticate(email, password) user = find_by_email(email) if user && user.password_hash == BCrypt::Engine.hash_secret ? (password, user.password_salt) user else nil end end =====flashメッセージの表示のための編集 /app/views/layouts/application.html.erb を以下のように編集 <!DOCTYPE html> <html> <head> <title>Auth</title> <%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %> </head> <body> <% flash.each do |name, msg| %> <%= content_tag :div, msg, :id 2011/11/25 以下は Ruby on Rails 3.1.3 をインストールしたときのログです. ====前準備 $ sudo aptitude install gcc make zlib1g-dev libreadline6-dev libssl-dev sqlite3 libsqlite3-dev g++ ====Ruby 1.9.2 http://www.ruby-lang.org/ja/ で最新安定版のRuby 1.9.3-p0をダウンロード $ wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p0.tar.bz $ tar -xvfz ruby-1.9.3-p0.tar.bz コンパイル/インストール(/opt/ruby へインストール) # mkdir -p /opt/ruby # cd /tmp/ruby-1.9.3-p0 # ./configure --prefix=/opt/ruby # make # make install /etc/profile へ以下を追加 # PATH="・・:/・・:/・・:/opt/ruby/bin" 一回reboot /root/.bashrc に以下を追加 export PATH=/opt/ruby/bin:$PATH ====Ruby on Rails 3 インストール # gem install rails libyaml を入れて再インストールしろと怒られたが,なんか入ってるっぽい. $ rails new *** でlibyaml を入れて再インストールしろと怒られた なので以下の手順で入れる(参照:http://d.hatena.ne.jp/donbulinux/20111111) # wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz # tar -zxvf yaml-0.1.4.tar.gz # cd yaml-0.1.4 # ./configure # make;make install その後, # mkdir -p /opt/ruby # cd /tmp/ruby-1.9.3-p0 # ./configure --prefix=/opt/ruby # make # make install をした. $ rails new test1 Webrick を立ち上げる $ cd test1 $ rails s /opt/ruby/lib/ruby/gems/1.9.1/gems/execjs-1.2.4/lib/execjs/runtimes.rb:45:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable) と怒られた Gemfile に gem 'therubyracer' を加えて $ rails new test2 bundle installでg++が必要と怒られる $ sudo aptitude install g++ $ cd test2 $ bundle install で成功!!!!!
2011/12/02 梅本 隆史 更新