我有一個文件列表,我試圖得到一個相對路徑如何在Rails 3.2中獲得相對路徑?
file = "/Users/yves/github/local/workshop/public/uploads/craftwork/image/1/a1d0.jpg"
Rails.public_path => "/Users/yves/github/local/workshop/public"
#我想獲得=> 「上傳/工藝品/圖像/ 1/a1d0.jpg」
file.relative_path_from(Rails.public_path) # is wrong
# raising : undefined method `relative_path_from' for #<String (file is a String..)
# so I tried to use Pathname class
Pathname.new(file).relative_path_from(Rails.public_path)
# but the I get another error
# undefined method `cleanpath' for String
Rails 3.2中不贊成使用relative_path_from嗎?如果是的話,現在有什麼好的?
我想'relative_path_from'是在'Pathname'一個Ruby方法;並不知道Rails將其添加到字符串(並不意味着它沒有,但我沒有看到它在任何地方)。 AFAIK它期望另一個路徑名,而不是一個字符串,因爲它在它的參數上調用了'cleanpath'。 –