0

Is there a possibility to rename directory from app?

I'm uploading files via FTP (cause it's a lot of files and it's weight) and naming directory with theme {model.title}-{model.place}. When I change the subject title or place in app, the directory name now is wrong and I must rename it by connecting to FTP. I want to automatically rename this dir when i hit "edit" in my app.

Got app in rails 3.1.3 with carrierwave.

4

2 回答 2

2

您可以使用FileUtils重命名 Ruby 中的目录

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
于 2012-06-07T08:10:12.417 回答
1

我认为用不变的东西命名你的目录会更有意义。为什么不直接使用 Model.id?

于 2012-06-07T10:29:51.747 回答