ブログ作り

RoR project

ブログ作り
 http://d.hatena.ne.jp/keyesberry/20070614/p1
を参照.
# rails brablog

$ cd brablog
$ ./script/server
ここで, ./script/server ができない.
→ rubygems0.2.4 にアップグレード.

すると
エラー: Missing the Rails 2.1.0 gem. Please 'gem install -v=2.1.0 rails',
update your RAILS_GEM_VERSION setting in config/environment.rb
for the Rails version you do have installed, or comment out RAILS_
GEM_VERSION to use the latest version installed.
と出たので,
# gem install -v=2.1.0 rails
でアップグレード.

                                                            • -

とりあえず・・・
1. rails プロジェクト todo_app を mysql で立ち上げる.
$ rails -d mysql todo_app
2. ディレクトリを移動 (以後の作業は $~/todo_app/ 内で行う).
$ cd todo_app
3. データベースの用意 (mysql を起動).
$ mysql -u root
4. データベースの作成 (mysql 内rakで)
mysql> create database todo_app_development character set utf8;
mysql> quit
5. 雛形を作成
$ ruby script/generate scaffold Todo name:string title:string description:text
due:datetime done:boolean
6. migration を有効にする.
$ rake db:migrate
7. サーバを起動
$ ruby script/server
8. ブラウザを起動
http://localhost.3000/todos
9. Time Zone の変え方
$ /todo_app/config/environment.rb
の中の
config.time_zone = 'UTC'
コメントアウトすると LT になる.

                                                                                              • -

用語集
scaffold: アプリケーション作成の基本的な枠組みとなるコードとして、テーブルの CRUD 操作
とそれに関連する画面表示のコードを生成してくれるオプションのこと.

                                                                                              • -

ログイン機能作成
1. プロジェクトを立ち上げる.
$ rails -d mysql login
2. データベースの作成.
$ mysqladmin -u root create project_development
3. プラグインのダウンロードとインストール.
$ ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/act_as_authenticated
4. マイグレーションスクリプト, モデル・コントローラー・ビューの作成
$ ruby script/generate authenticated user account
5. マイグレーションを実行.
$ rake db:migrate
6. サーバの起動.
$ ./script/server
7. ログイン.
http://localhost:3000/account/signup

                                                                                                • -

既に存在するプロジェクトにログイン機能を持たせる.
1. プラグインのダウンロードとインストール.
$ ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated
2. マイグレーションスクリプト, モデル・コントローラー・ビューの作成
$ ruby script/generate authenticated user account
3. マイグレーションを実行.
$ rake db:migrate
4. サーバの起動.
$ ./script/server
5. ログインしてみる.
http://localhost:3000/account/signup
http://localhost:3000/account/login
6. ログイン画面から目的の場所へ移動する.
$ /app/controllers/account_controller.rb
内の,
(:controller => '/account', :action => 'index' )
の"/account" を目的のあるディレクトリ名に, "index" の部分を目的のファイル名に変える.
(3 箇所あるので全て変える.)
7. 再びログインする.
http://localhost:3000/account/signup
http://localhost:3000/account/login

                                                                                                • -

restful_authentication を用いたユーザー認証
1. プラグインのダウンロードとインストール
$ ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication
2. マイグレーションスクリプト, モデル・コントローラー・ビューの作成
$ ruby script/generate authenticated user sessions
3. apprication.rb の編集
$ /app/controllers/apprication.rb
内に,
include AuthenticatedSystem
を追加.
4. users_controller.rb, sessions_controller.rb の編集
$ /app/controllers/users_controller.rb

$ /app/controllers/sessions_controller.rb

include AuthenticatedSystem をコメントアウト
5. 認証が必要となるようにする.
ユーザー認証が必要なコントローラーに
before_filter :login_required
と記述する.

参考
http://d.hatena.ne.jp/spitfire_tree/20080221/1203610276

                                                                                                  • -

ユーザー管理
http://d.hatena.ne.jp/zariganitosh/20080730/1217395413
参照.

                                                                                                  • -

書き込みの制約
1. destroy を消す.
$ /app/views/haizokus/index.html.erb
を編集.
Destroy の部分をコメントアウト.
2. 一度記入すると新規作成をできないようにする.
$ /app/controllers/haizokus_contlloers.rb
を編集.

def new
@kus = current_users.haizokus
if @kus.empty?
@haizoku = Haizoku.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @haizoku }
end
else
redirect_back_or_default('/out.html')
end
end

3. out.html を作成.
$ /public/out.html を作成
書き込みの制約の旨を記入して, 戻るボタン等を付ける.
戻るボタン

                                                                                      • -

migration の変更
1. 変更用のマイグレーションファイルを作成.
$ruby script/generate migration Change_column
2. 生成されたマイグレーションファイルを編集.
class ChangeColumn < ActiveRecord::Migration
def self.up
change_column( :haizokus, :pplab, :integer) #←( :tablename, :columnname, :… )
… #(以下変更したい項目を同様に変える)
end

def self.down
raise ActiveRecord::IrreversibleMigration
end
end
3. マイグレーションを有効にする.
$rake db:migrate

                                                                                        • -

logout 機能
1. /app/views/layouts/****.html.erb を編集.


<%= link_to_if logged_in?, 'Logout >>', session_path, :method => :delete do
end %>

                                                                                        • -

集計システム
1. summaries ページの作成.
$ ruby script/generate scaffold Summary
2. summaries_controller.rb の編集
Bunzokus クラスの first, second, third, fourth, fifth の数を取り出す.
・・・
def index
@first =
@second =

@third =
@fourth =

@fifth = []
@labo = ["","hogehoge", "hogehoge2",・・・,"hogehogen"]
1.upto(18) do |i|
@first[i] = Bunzoku.count(:first, :conditions => ['first = ?',i])
@second[i] = Bunzoku.count(:second, :conditions => ['second = ?',i])
@third[i] = Bunzoku.count(:third, :conditions => ['third = ?',i])
@fourth[i] = Bunzoku.count(:fourth, :conditions => ['fourth = ?',i])
@fifth[i] = Bunzoku.count(:fifth, :conditions => ['fifth = ?',i])
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @otameshi1s }
# format.xml { render :xml => @opp }
end
end
3. index.html (summaries) の編集.

集計

<% 1.upto(18) do |i|%> <% end %>
研究室名 第1希望 第2希望 第3希望 第4希望 第5希望
<%= @labo[i] %>
<%= @first[i] %>

<%= @second[i] %>

<%= @third[i] %>

<%= @fourth[i] %>

<%= @fifth[i] %>
4. 各ページに研究室一覧を作成.
4.1. bunzokus_controller の編集.
各 def にハッシュを作成.
def hogehoge #hogehoge は index, show, new, edit
@labo = ["", "hogehoge", "hogehoge2",・・・, "hogehogen"]
・・・
4.2 index.html, new.html, show.html, edit.html を編集.
・・・

研究室一覧

<% 1.upto(9) do |i| %> <% end %> <% 1.upto(9) do |i| %> <% end %>

<%= i %>

<%= @labo[i] %>
<% 10.upto(18) do |i| %> <% end %> <% 10.upto(18) do |i| %> <% end %>

<%= i %>

<%= @labo[i] %>

                                                                                        • -

mongrel のインストール.
1. build-essential と devscripts ruby1.8-dev のインストール.
$sudo apt-get install build-essential devscripts ruby1.8-dev
2. mongrel のインストール.
$ sudo gem install -y mongrel
最新版の ruby を選択.
何か忘れたが, ruby を選択.
3. 起動.
$ ./script/server
で起動.