rails:1112
From: yosuke@j...
Date: Sat, 06 May 2006 02:19:25 +0900
Subject: [rails:1112] エラー処理について
皆様
suzukiと申します。
よろしくお願いします。
現在、controllerとして後段に添付するような
メソッドを書いていますが、エラー処理で悩んでいます。
先に出てくるエラー処理のためのbeginでは、
ファイルが読み込めない場合のエラー処理を
書いたつもりですが、不正なパラメータを読み込ませても
うまくindexにリダイレクトされません。
不正なパラメータを読み込ませた場合
for images in f3
という行について、
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.each
と怒られてしまいます。
2回目に出てくるディレクトリが存在するか否かの
エラー処理のためのbeginはうまく動いています。
railsというよりrubyの文法の問題なのかもしれませんが、
もし原因がわかる方いらっしゃいましたらご教示いただけると
助かります。
#そもそも、こういう風にだらだらとコードを書くのは
#Railsの思想に合ってない気もしています。
<ここからコード>
コード自体は、galleryというphpのアプリで管理している
画像を別のディレクトリにコピーして使うためのものとして
作成中です。
def readdatfile
baseDir = "/var/www/albums/"
tempDir = "/var/tempPhoto/"
targetDir = params[:targetDir]
targetDirPath = baseDir + targetDir + "/"
targetFilePath = targetDirPath + "photos.dat"
begin
f = open(targetFilePath)
rescue
logger.error("datファイルを読み込めません")
flash[:notice] = 'datファイルを読み込めません。パラメータを確認してくださ
い'
redirect_to(:action => 'index')
else
f2 = f.read
f3 = PHP.unserialize(f2)
end
#ディレクトリの作成
newDirPath = tempDir + targetDir
begin
FileUtils.mkdir(newDirPath)
rescue
@messageMkdir = newDirPath + "は存在しています"
else
@messageMkdir = newDirPath + "を作成しました"
end
#画像サイズの配列
fileSizes = ['','.sized','.thumb']
#f3のphotos.datをunserializeした配列をループ
for images in f3
#さらに画像サイズの配列でループ
for fileSize in fileSizes
cpImagePath = targetDirPath + images.image.name + fileSize + ".jpg"
FileUtils.cp(cpImagePath, newDirPath)
end
end
#画像リストをViewに渡すための代入
@outputF = PHP.unserialize(f2)
end
--
ML: rails@r...
使い方: http://QuickML.com/
-> 1112 2006-05-05 19:19 [yosuke@j... ] エラー処理について 1114 2006-05-06 05:21 ┗[babie7a0@y... ] 1115 2006-05-06 06:08 ┗[yosuke@j... ]