2012-06-07 22 views
0

是否有可能從app重命名目錄?從應用程序重命名目錄[Rails,Carrierwave]

我通過FTP上傳文件(因爲它有很多文件和它的重量),並命名主題爲{model.title} - {model.place}。當我更改主題標題或在應用程序中的位置時,目錄名稱現在是錯誤的,我必須通過連接到FTP來重命名它。我想在我的應用程序中點擊「編輯」時自動重命名此目錄。

在載波導航欄3.1.3中獲得應用程序。

回答

2

您可以在Ruby中與FileUtils

require 'fileutils' 
FileUtils.mv old_directory_name, new_directory_name 

重命名目錄你可以用模型回調實現這一點:

class MyModel < ActiveRecord::Base 
    # Callback triggered by a changed place or title 
    before_save :change_directory_names 

    private 

    # Method that changes directory names 
    def change_directory_name 
    if self.title_changed? 
     title = self.title.changes.flatten.drop(1) 
     # Code here to change the directory name 
     # Old title: title.first 
     # New title: title.last 
    elsif self.place_changed? 
     place = self.place.changes.flatten.drop(1) 
     # Code here to change the directory name 
     # Old place: place.first 
     # New place: place.last 
    end 
    end 
1

我想這更有意義的名字的東西你的目錄,沒有按不會改變。爲什麼不使用Model.id?