找到你要的答案

Q:Rails 4, Paperclip, Upload in FTP. How to rename?

Q:Rails 4,回形针,上传到FTP。如何重命名?

I'm using Rails 4 and Paperclip.

Beacuse I need to upload files on FTP server i'm using this great gem:

https://github.com/xing/paperclip-storage-ftp

Everything works perfect in local, but in FTP I can't rename files using this code:

  def rename_myfile
    if self.rename.present?
      path = self.myfile.path
      FileUtils.move(myfile.path, File.join(File.dirname(myfile.path), self.rename))
      self.myfile_file_name = self.rename
    end
  end

I got an error:

No such file or directory @ sys_fail2 - (/myfiles/19/original/myfileOriginalName.jpg, /myfiles/19/original/myfileRenamedName.jpg)

How to enter in ftp with FileUtils.move???

Create and Delete are working very well!

https://github.com/xing/paperclip-storage-ftp/issues/28

我使用Rails 4和回形针。

因为我需要上传的文件在FTP服务器我使用这个伟大的宝石:

https://github.com/xing/paperclip-storage-ftp

一切都完美的地方,但我不能重命名FTP文件使用此代码:

  def rename_myfile
    if self.rename.present?
      path = self.myfile.path
      FileUtils.move(myfile.path, File.join(File.dirname(myfile.path), self.rename))
      self.myfile_file_name = self.rename
    end
  end

我有一个错误:

没有这样的文件或目录”sys_fail2 -(/我的文件/ 19 /原/ myfileoriginalname.jpg,/我的文件/ 19 /原/ myfilerenamedname .jpg)

如何进入FTP fileutils.move???

创建和删除工作非常好!

https://github.com/xing/paperclip-storage-ftp/issues/28

answer1: 回答1:

You have to build the full path to the file not just the file's dirname and name. change your FileUtils.move line to this:

orig_full_path = Rails.root.join "public", myfile.path # assuming they're saved in your public directory
new_full_path = Rails.root.join "public", File.dirname(myfile.path), self.rename
FileUtils.move orig_full_path, new_full_path

The idea here is to get the absolute path to your files. Before you were just giving FileUtils this path: /myfiles/19/original/myfileOriginalName.jpg which means it will look for the file in a folder /myfiles in the root of your file system. But they're actually in your Rails folder. So you should use Rails.root.join to get the true absolute path: /Users/me/my_rails_project/public/myfiles/19/original/myfileOriginalName.jpg.

你必须建立完整的文件路径不只是文件的目录和名称。改变你的fileutils.move线这:

orig_full_path = Rails.root.join "public", myfile.path # assuming they're saved in your public directory
new_full_path = Rails.root.join "public", File.dirname(myfile.path), self.rename
FileUtils.move orig_full_path, new_full_path

这里的想法是获得绝对路径到您的文件。以前你只是给这条路径:/ FileUtils MyFiles / 19 /原/ myfileoriginalname.jpg这意味着它将寻找一个文件夹/我的文件在你的文件系统的根文件。但它们实际上在你的Rails文件夹中。所以你应该使用rails.root.join获得真正的绝对路径:/用户/我/ my_rails_project /公共/我的文件/ 19 /原/ myfileoriginalname.jpg。

ruby-on-rails  ruby-on-rails-4  ftp  paperclip  rails-activerecord