目次

2009年1月27日火曜日

Ruby on Rails チュートリアル(3) 補足:ログアウト機能とeditとnew

チュートリアル(3)ではログアウトメソッドはあったものの、viewに出力していなかったためログアウト出来ない状態でした。また、editとnewも、ログイン名とパスワードを追加して、使えるようにしなくてはなりません。

まずはログアウトを、user/indexから出来るようにします。

view>users>index.rhtml.erbに以下のコードを追加。
<% if @current_user -%>
<%= @current_user.user_name %> さんのアカウント |
<%= link_to 'ログアウト',
{ :controller => '/login', :action => 'logout' },
{ :method => 'post',
:confirm => '本当にログアウトしますか?' } -%>
<% end -%>



新しいユーザーを追加出来るようにnew.rhtml.erbとedit.rhtml.erbの編集。それぞれに以下のコードを追加します。
<p>ログイン名: <%=form.text_field :login_name, :size => 16 %></p>
<p>パスワード: <%=form.password_field :password, :size => 16%></p>


これだけではまだ使えません。models>user.rbを編集。以下のコードを追加。
attr_accessor :password, :password_confirmation
attr_protected :salt, :hashed_password

# パスワードが送られたとき
def password=(pw)
@password = pw
if pw and pw.size > 0
self.salt = rand(100000000)
self.hashed_password = User.hashed_password(pw, self.salt)
end
end


これで完成!


おまけ:
プロジェクト全体のindexページを変えたい場合、つまり「Welcome aboard」ページ以外をトップにしたい場合の話。
まずpublic>index.htmlを削除して、
config>routes.rbに以下のコード追加。
map.root :controller => "users"

これで、http://localhost:3000にアクセスした時、user indexがトップになります。

Ruby on Rails (4) ~テーブル同士の連携、1対多編~

今回は複数のテーブルを連携させてみましょう。
前に作ったtest003(チュートリアル(2))を使います。

songというテーブルを作って、userテーブルで読み込みます。

まずはmodelの作成
ruby script/generate model song

XXXXXX_create_songsを編集します。
class CreateSongs < ActiveRecord::Migration
def self.up
create_table :songs do |t|
t.column :song_name, :string # 曲名
t.column :artist_name, :string # アーティスト
t.column :song_url, :string # URL
t.timestamps
end
end

def self.down
drop_table :songs
end
end


次はymlの編集。フィクスチャファイルsongs.ymlをdb>fixtures>decelopmentに作ります。
# 開発環境のためのフィクスチャ: songs
one:
id: 1
song_name: test1
artist_name: さとう # アーティスト
song_url: http://google.co.jp # URL
created_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
updated_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>

two:
id: 2
song_name: test2
artist_name: やまだ # アーティスト
song_url: http://google.co.jp # URL
created_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
updated_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>

three:
id: 3
song_name: test3
artist_name: すずき # アーティスト
song_url: http://google.co.jp # URL
created_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
updated_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>


完成したら
rake db:initialize

では、連携させましょう。
app>modelsの中のuser.rbとsong.rbをそれぞれ編集します。

user.rb
class User < ActiveRecord::Base
belongs_to :song
end


song.rb
class Song < ActiveRecord::Base
has_many :users
end

belopgs_toで、そのデータベースからは1つのデータとしか対応していないことを表し、has_manyで複数のデータと対応していることを示します。
song対userが1対多の関係になっています。1対1ではないのは、同じ曲を聴いている人もいるからです。
しかし、これだけではまだまだ。外部キーというものをテーブルの中につくって、ようやくテーブルからテーブルへデータを引き渡せるのです。
マイグレーション用のモデル、XXXXXXXX_create_users.rbを編集して、外部キーを新しく設けます。
t.column :listening_song, :string
ここを消して、
t.column :song_id, :integer # 外部キー
これを代入します。

これでuserを通してsongのデータベースを引っ張って来れるようになりました。

最後にviewの編集です。
といってもこれまで
@user.listening_songと記述していた所を、
@user.song.song_nameと変えるだけです!
テーブル名.テーブル名.フィールド名でデータを持って来れるという簡単さ!!!!

2009年1月26日月曜日

Ruby on Rails チュートリアル(3) ~pluginを使わずにログイン機能を作ってみよう~

ちょこっとやってみたらものの数分でログイン/セッション機能が完成して、とても嬉しくなりました。プラグインのおかげです。railsって本当に素晴らしい。

が、これではいつまでたってもRailsでのログイン設計を理解できるようになりません。悩みましたが、プラグインを使わずにログイン機能を作るチュートリアルにします!長くなりますが、頑張りましょう!!!このブログの順番がごちゃごちゃですがtest003(チュートリアル(4))を使います。失敗する恐れもありますので、同じ手順でtest004を今回新たに作り直しました。


ログインしないと、ユーザーデータが見れないようにします。

ログイン機能を作るための手順です。
(1) テーブルにフィールドを追加する(ログイン名とパスワード)
(2) user.rb modelの編集
(3) ymlの編集
(4) login controllerとview(ログインのindexページ)を作る
(5) application.rbの編集
(6) users_controllerの編集

さあ張り切って行きましょう。

(1) テーブルにフィールドを追加する(ログイン名とパスワード)
ログイン名とパスワード追加用のマイグレーションファイルを作ります。
$ ruby script/generate migration alter_users
中身はこんな感じです。パスワードは暗号化します。

db>migrate>XXXXXXXX_alter_users.rb
class AlterUsers < ActiveRecord::Migration

def self.up
add_column :users, :login_name, :string, # ログイン名
:null => false
add_column :users, :hashed_password, :string
# 暗号化されたパスワード
add_column :users, :salt, :string # パスワードソルト
end

def self.down
remove_column :users, :login_name
remove_column :users, :hashed_password
remove_column :users, :salt
end


end


(2) user.rb modelの編集
入力されるパスワードを暗号化する=ハッシュ値を作るため、modelを編集します。
app>models>user.rb
class User < ActiveRecord::Base
belongs_to :song

attr_accessor :password, :password_confirmation
attr_protected :salt, :hashed_password

def self.authenticate(login_name, password)
user = find_by_login_name(login_name)
if user and user.hashed_password ==
hashed_password(password, user.salt)
user
else
nil
end
end

# ハッシュ値の生成
def self.hashed_password(password, salt)
Digest::SHA1.hexdigest(sprintf("%s%08d", password, salt))
end

end


(3) ymlの編集
フィクスチャデータを編集。
この三行を追加してください。

user.ymlに追加

login_name: user1(userごとに、ここは変える必要があります。)
hashed_password: <%= User.hashed_password('password', 12345678) %>
salt: 12345678


ここまで来たら、データの投入です。
rake db:migrate

(4) login controllerとview(ログインのindexページ)を作る
それでは、ログイン用のフォームを作りましょう。
$ ruby script/generate controller login index

login_controller.rb
class LoginController < ApplicationController

# ログイン
def login
login_name = params[:login_name]
password = params[:password]
user = User.authenticate(login_name, password)
if user
session[:user_id] = user.id
redirect_to :controller => 'users', :action => 'index'
flash[:notice] = "Logged in successfully"
else
session[:user_id] = nil
redirect_to :controller => 'login', :action => 'index'
flash[:warning] = 'ログイン失敗'
flash[:login_name] = login_name
end
end

# ログアウト
def logout
session[:user_id] = nil
redirect_to :controller => 'login', :action => 'index'
end

end


login>index.rhtml.erb

<% unless @current_user -%>
<div>
<h1>ログイン</h1>
<% form_tag '/login/login', :method => 'post' do %>
<%= hidden_field_tag 'from', request.request_uri %>
<table border="0">
<tr><td align="right">ユーザー名:</td>
<td><%= text_field_tag 'login_name', flash[:login_name],
:size => 16 %></td></tr>
<tr><td align="right">パスワード:</td>
<td><%= password_field_tag 'password', '',
:size => 16 %></td></tr>
<tr><td colspan="2" align="center">
<%= submit_tag 'ログイン' %>
</td></tr>
</table>
<% end %>
</div>
<% end -%>

<% if flash[:warning] -%>
<div id="warning">
<%= flash[:warning] %>
</div>
<% end -%>


(5) application.rbの編集
ログイン状態の確認を、すべてのコントローラーで使うため、application.rbを編集します。

controller>application.rb
class ApplicationController < ActionController::Base
helper :all

protect_from_forgery # :secret => 'b9df6040833a6d4befdb3d8b5fa52082'

before_filter :resume_session

private
# セッションの再開
def resume_session
return unless session[:user_id]

begin
@current_user = User.find(session[:user_id])
rescue ActiveRecord::RecordNotFound
session[:user_id] = nil
end
end

# ログインしていないユーザーをはじく
def block_non_users
unless @current_user
flash[:warning] = 'ログインが必要です。'
redirect_to :controller => 'login', :action => 'index'
return false
end
end
end


(6) users_controllerの編集

class UsersController < ApplicationControllerの下に以下のコードを追加します。
before_filter :block_non_users


これで完成!serverを立ち上げて、usersに接続しようとすると、ログインページに飛ばされるはずです。

2009年1月23日金曜日

Ruby on Rails チュートリアル(2) 〜投稿、編集、削除まで〜

前回作ったものを元に、新しいユーザーを追加したり、情報を編集したり、削除したりできるようにします。

さっそく作ろう、という前にちょっとお話。
データベースの基本操作はCRUD/Create, Read, Update, Deleteの4つから成り立っているそうです。この4つをRailsで実現するには7つのアクションを用意するのが一般的。
7つ・・・・・index, show, new, edit, create, update, destroy
なんとなく名前から、それぞれがどんな役割を担うか分かると思います。
それでは「user_controller.rb」の編集から。


class UsersController < ApplicationController
def index
@users = User.find(:all)
end

def show
@user = User.find(params[:id])
end
#indexとshowは前回と同じです。
def new
@user = User.new
end
 #モデルクラス名.newで新しいレコード作成
def edit
@user = User.find(params[:id])
end
#showと同じ
#新規作成用フォーム
def create
@user = User.new(params[:user])#上のnewでの値を受け取っています。
if@user.save
flash[:notice] = '新しいユーザーが追加されました!'
redirect_to :action => 'show' , :id => @user #処理が終わったら新規作成したユーザーのshow/(id)へ
else
render :action => 'new'
end
end
 #flashで、画面に処理表示の結果を出力することができます。
def update
@user = User.find(params[:id])
@user.attributes = params[:user]
@user.save
if@user.save
flash[:notice] = '情報が更新されました'
redirect_to :action => 'show' , :id => @user#処理が終わったら,更新したページ=show/(id)へ
else
render :action => 'edit'

end
end
 #editの値をうけとってupdateしています。
def destroy
@user = User.find(params[:id])
@user.destroy
flash[:notice] = '削除しました'
redirect_to :action => 'index'#処理が終わったらindexへ
end

end



7つのアクションを編集しました。
次はそれぞれのViewを編集していきます。

まずはindex.rhtml.erb


<h1>Test003: Users : indexページ</h1>

<div class ="notice"><%= h(flash[:notice])%></div>#Flashによる、処理結果の表示
<p><%= link_to('新規ユーザー作成', :action => 'new')%></p>


<% @users.each do |user| -%>
<li>
<%= link_to(h(user.user_name), :action => 'show', :id => user.id)%>
<%= link_to('編集', :action => 'edit', :id => user.id)%>
<%= link_to('削除', {:action => 'destroy', :id => user.id},
{:method => :post, :confirm => '本当に削除しますか?'})%>

</li>
<% end -%>





次に、edit.rhtml.erb


<h1>Members#edit#ユーザーデータを編集する</h1>

<% form_for :user, @user,
:url=>{:action => 'update', :id => @user} do |form| %>

<p>名前: <%=form.text_field :user_name, :size => 16 %> </p>
<p>曲: <%=form.text_field :listening_song, :size => 16 %> </p>
<p>メッセージ:<br /><%=form.text_area :message, :colse =>40, :rows => 3%> </p>

<p>
<%= form.submit '更新' %>
</p>
<% end %>
<p><%= link_to('一覧へ戻る', :action => 'index') %></p>




次はnew.rhtml.erb


<h1>Users#new#新しいユーザーを作る</h1>

<% form_for :user, @user,
:url=>{:action => 'create'} do |form| %>

<p>名前: <%=form.text_field :user_name, :size => 16 %> </p>
<p>曲: <%=form.text_field :listening_song, :size => 16 %> </p>
<p>メッセージ:<br /><%=form.text_area :message, :colse =>40, :rows => 3%> </p>

<p>
<%= form.submit 'Create' %>
</p>
<% end %>
<p><%= link_to('一覧へ戻る', :action => 'index') %></p>




これで完成。
私がつまずいていたのは
form_for
の記述方法。URL => 'アクション名'は、editはupdate/newはcreateになります。

補足:チュートリアル(1) もう一回フィールドを作り直したい場合

前回のblogが大変長くなりましたので、マイグレーション関係の補足。
一度、migrateしてしまうと、簡単にはやりなおせません。
方法は3つ
1,DBを一から作り直す
2,バージョンを増やしてマイグレーションスクリプトを作り直した後、マイグレーションを行う。
3,initializing.rakeという初期化用ファイルを使う

1,の場合
mysqlでデータベースを削除(drop データベース名;)した後、もう一度データベースを作り、マイグレーションし直す。めんどくさい(笑)

2,の場合
002_テーブル名.rbと名前をつけてマイグレーションするというもの。しかし、rails2.0以降、この仕様は変更されたので、上手くいくか不明。未確認です、ごめんなさい(苦笑)

3,の場合
これも本にのっていたスクリプトを使ってちょいっとやっちゃいます。1,を自動でやってくれるrakeタスクです。一番簡単ですが、これをするとフィクスチャファイル以外の、これまで蓄積されたデータがパァになってしまうので注意。
lib>taskフォルダの下に以下のソースを「initializing.rake」という名前で保存しましょう。


namespace :db do
desc "Create the development and test databases."
task :create do
require 'yaml'

path = File.dirname(__FILE__) + "/../../config/database.yml"
configurations = YAML.load_file(path)

%w(development test).each do |env|
config = configurations[env]
if config['adapter'] == 'mysql'
command = sprintf(%{mysql --user %s}, config['username'])
command += " --password=#{config['password']}" if config['password'] and !config['password'].empty?
command += " --host #{config['host']}" if config['host'] and !config['host'].empty?
command += " --port #{config['port']}" if config['port'] and !config['port'].empty?
sh command + %( --execute="drop database if exists #{config['database']}")
if config['encoding'] and !config['encoding'].empty?
sh command + %( --execute="create database #{config['database']} default character set #{config['encoding']}")
else
sh command + %( --execute="create database #{config['database']}")
end
elsif config['adapter'] == 'postgresql'
# 未実装
else
raise "Unsupported database adapter #{config['adapter']}"
end
end
end

desc "Create and initialize the development and test databases."
task :initialize => ["db:create", "db:migrate", "db:import:development", "db:test:prepare"]
end




ファイルを準備した後は、
rake db:initialize
これでOK!

Ruby on Rails チュートリアル(1) ~Scaffoled未使用!DB作成からViewまで~

今まで本を見ながらやっていた事のおさらいもかねて、チュートリアルを書いてみようと思います。Rails2.0以降、scaffoldが便利すぎて、自分で一からデータベースを作って呼び出すということを詳細に書いてあるチュートリアルがない!
と苦悩しました。
いろいろなblogを参考にしながら私もひとつ書いてみようじゃないか、と思った次第であります。

今回作るのはこんなもの。


メンバーの一覧が出て来て、名前をクリックすると詳細ページに飛べるってものです。デザインもへったくれもありませんが、これを作って行きましょう。
test002というプロジェクトを作って、対応するDBもつくって、、、、って感じですね。

<1>プロジェクトを作る。
$ rails -d mysql test002
いつもと同じく、です。-d mysqlはrails 2.0以上からはデフォルトDBがsqliteになってしまっているため、忘れずに行いましょう。mysqlで作っている理由は「慣れ」です(笑)

<2>DBを作る。
$ mysql -u root
mysql> create database test002_development;
mysql> create database test002_test;
DB完成。
mysql> exit
mysqlパートは終了です。

<3>モデルを作る。(Userモデルを作ります)
<1>で作ったプロジェクトのディレクトリに移動。
$ cd test002
$ ruby script/generate model user
これでdb>migrateの中にXXXXXXXX(作成日)_create_users.rbというファイルが出来ます。
usersと複数形になるのは仕様です。このusersというのがテーブルの名前になります。

<4>マイグレーション
テーブルの中身"フィールド"というものを作ります。
先ほど作ったモデル「XXXXXXXX(作成日)_create_users.rb」をエディタでOPEN。



class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :user_name, :string, # 名前(姓)
:null => false
t.column :listening_song, :string # 聴いている曲
t.column :message, :text # 備考
t.column :created_at, :datetime # 登録日時
t.column :updated_at, :datetime # 更新日時

t.timestamps
end
end

def self.down
drop_table :users
end
end



今回は私が作っているMuliusの仕様に合わせて、名前と聴いている曲などのフィールドを足しました。お好みで!
:null => false
これが書いてあるフィールドは、そのフィールドが埋まっていないとエラーが出ます。注意!

<5>マイグレーション
では作ったフィールドをmysqlのDBに取り込みましょう。
$ rake db:migrate

下の結果が出れば、成功。


(in /Users/suzukiyasuko/rails/test002)
== CreateUsers: migrating ====================================================
-- create_table(:users)
-> 0.0048s
== CreateUsers: migrated (0.0053s) ===========================================


エラーが出ないのに、この結果がでない場合、すでに一度マイグレーションしていると思われます。もう一回フィールドを作り直したい場合については次回のblogでまとめておきます。どうしてもって、いう場合は、mysqlでDBを破棄して、<5>をやり直してください。
成功しているか気になる人は、mysqlで
mysql> use test002_development;

mysql> desc users;


+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_name | varchar(255) | NO | | NULL | |
| listening_song | varchar(255) | YES | | NULL | |
| message | text | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)


こんな風に表示されれば大丈夫!

<6>フィクスチャファイルを作る
テーブルと、その中身フィールドを作ってもデータが入っていないのでからっぽです。
フィクスチャファイルを使って、仮のデータを入れてみましょう。
テストを開始するためのソフトウェアの初期状態=フィクスチャらしいです。
まあ、テストのための仮のデータとでも思ってください。
dbフォルダの下にfixturesフォルダを作りましょう。この中に、さらにdevelopmentというフォルダと、productionというフォルダを作ります。developmentフォルダの中に「users.yml」(テーブル名.yml)というファイルを作ります。


# 開発環境のためのフィクスチャ: users
John:
id: 1
user_name: ジョンレノン
listening_song: 赤いスイートピー
message: 松田聖子イイ!!
created_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
updated_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>

Yauko:
id: 2
user_name: 鈴木泰子
listening_song: 木綿のハンカチーフ
message: 古い曲ばっかりだな
created_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
updated_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>



先ほど作ったテーブルの中身と違いの内容に注意。とくにnull =>falseの指定をしたものは、忘れずに書きましょう。

<7>データ投入
ここでちょっと飛び道具。rakeタスクを使って、データを取り込んじゃいましょう。
lib>tasksフォルダの中に、data_loading.rakeというタスクファイルを作ります。本に乗っていたものをそのまま作っちゃいます。


require 'active_record'
require 'active_record/fixtures'
namespace :db do
def load_data(env)
ActiveRecord::Base.establish_connection(env)
pattern = File.join(RAILS_ROOT, 'db', 'fixtures',
env.to_s, '*.{yml,csv}')
Dir.glob(pattern).each do |fixture_file|
table_name = File.basename(fixture_file, '.*')
Fixtures.create_fixtures('db/fixtures/' + env.to_s, table_name)
end
end

namespace :import do
desc "Load fixture data into the development database."
task :development => :environment do
load_data :development
end

desc "Load fixture data into the production database."
task :production => :environment do
load_data :production
end
end
end


完成したら
$ rake db:import:development
これでようやくDBにデータが投入出来ました。

<8>ビューとコントローラーを作る
$ ruby script/generate controller users index show
これでcontrokkerフォルダの中にはusers_controller.rbが出来ます。
viewフォルダの中にindex.rhtml.erbとshow.rhtml.erbも出来ています。
まずはusers_controller.rbの編集。


class UsersController < ApplicationController
def index
@users = User.find(:all)
end

def show
@user = User.find(params[:id])
end

end


次にindex.rhtml.erb


<% @users.each do |user| -%>
<li>
<%= link_to(h(user.user_name), :action => 'show', :id => user.id)%>
</li>
<% end -%>



さらに、show.rhtml.erb



<p>名前は:<%=@user.user_name%></p>
<p>聴いている曲は:<%=@user.listening_song %></p>
<p>メッセージ:<%=@user.message%></p>

<%= link_to '一覧表示に戻る', :action => 'index'%>



これで完成!
$ ruby script/server
さっそくブラウザで見てみましょう。
このチュートリアルが終わった後、もう一度こんどは
テーブルの名前やフィールドを自分で考えたもの、Viewで表示するフィールドを変えてみるなど、一から自分で作ってみると、Railsへの理解を深めることが出来ると思います。

大変おつかれさまでした!

2009年1月16日金曜日

Mulius WebApp ver1.0 作成

夜〜朝にかけて、ひたすらrailsでMulius Web Appのインターフェイスを作っていました。
htmlは得意だと、普段から嘯いていたくせにこの有様。。。


最初はapplication.rhtmlに「?」って感じだったけど、理解したあとだと、すいすい作成できました。
railsが好きになってきた!