回形针更改 URL/路径 [英] Paperclip changing URL/Path

查看:67
本文介绍了回形针更改 URL/路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Rails 4 Ruby 2.1.1

I am using Rails 4 Ruby 2.1.1

我需要更改:url、:path、:default 以便我可以访问控制器目录中的 example-data.csv 文件.目前它将我的 example_data.csv 文件存储在 public/origin/example_data.csv 中.但我希望我的 example_data.csv 文件保存在/controllers/original 目录中.

I need to change my :url, :path, :default so I can reach my example-data.csv file in the controllers directory. Currently it is storing my example_data.csv file in public/origin/example_data.csv. But I want my example_data.csv file saved in the /controllers/original directory.

这是我目前所拥有的.

has_attached_file :

csvdata, :url => "/controllers/original/:style/:basename.:extension",
         :path => ":rails_root/controllers/original/:style/:basename.:extension",
         :default_url => "/controllers/original/example_data.csv"

当我运行代码时,它不会将我的代码放在控制器目录中.

When I run the code it does not place my code in the controllers directory.

推荐答案

Paperclip 允许您在首选位置上传文件.您可以根据需要修改其选项,例如 :url:path:default_url.

Paperclip allows you to upload files at your preferred location. You can modify its options such as :url, :path, :default_url as you desire.

在我们进一步讨论之前,让我让您了解一下这些选项的用途:

Before we move further, let me give you little bit idea of what these options are for:

:url         - The full URL of where the attachment is publicly accessible. 

:path        - The files that are assigned as attachments are, by default, placed in the directory specified by this option.

:default_url - The URL that will be returned if there is no attachment assigned.

:styles      - A hash of thumbnail styles with geometries. If you need copies of uploaded files with particular dimensions then specify them here.

<小时>

让我们一步一步来:

您的第一个要求如下:


Lets take step by step approach here:

Your first requirement as below:

我需要更改我的 :url、:path、:default_url 以便我可以访问控制器目录中的 example-data.csv 文件.

是的,这是可能的.您当前的配置如下所示,无需任何更改即可正常工作.

Yes, its possible. Your current configuration as shown below would just work fine without any changes.

has_attached_file :csvdata, 
         :url => "/controllers/original/:style/:basename.:extension",
         :path => ":rails_root/controllers/original/:style/:basename.:extension",
         :default_url => "/controllers/original/example_data.csv"

但是这里有一些我想强调的问题.通过上述设置,每当您上传文件时,它都会存储在application_folder/controllers/original/original 目录下.路径 /original 的最后一部分归因于 :path 选项中指定的 /:style.由于您没有为附件指定任何 :styles 选项,因此只会创建默认样式,即 original.请注意controllers 文件夹位于应用程序根级别,这是一个不可公开访问的文件夹.

BUT there are some gotchas here which I would like to highlight. With the above setup, whenever you upload a file, it will be stored under application_folder/controllers/original/original directory. The last part of path /original is due to /:style specified in :path option. As you have not specified any :styles option for the attachment, only default style i.e., original will be created. Please note that the controllers folder resides at application root level which is a non-publicly accessible folder.

只有在 public 文件夹(及其后代)下上传的文件可以通过网络浏览器访问.如果您不打算在视图中的任何位置显示上传的文件和/或只需要它进行某些后台处理,那么您当前的配置不需要任何更改.它会正常工作.

Only the files uploaded under public folder(and its descendants) are accessible via the web browser. If you don't plan to display the uploaded files anywhere in your view and/or just need it for some background processing, then, your current configuration does not require any change. It will work fine as it is.

如果这是您打算实现的目标,则无需采取进一步行动.

If this is what you intended to achieve then no further action required.

但是如果您打算在网络应用程序中显示或访问上传的文件,那么您需要将其放置在可通过浏览器访问的 public 文件夹中.在这种情况下,您的代码需要进行一些更改.

BUT if you are planning to display or access the uploaded file within a web application then you would need to place it within public folder which is accessible via browser. In that case, your code would need some changes.

## Update current configuration as below 
has_attached_file :csvdata, 
         :url => "/controllers/:style/:basename.:extension",
         :path => ":rails_root/public/controllers/:style/:basename.:extension",
         :default_url => "/controllers/:style/example_data.csv"

注意:我从路径中删除了 /original ,因为 :style 将创建 original 文件夹.否则,文件夹将是 /controllers/original/original/..

NOTE: I removed /original from the path as :style would create original folder. Otherwise, folders would be /controllers/original/original/..

通过上述设置,每当您上传文件时,它都会存储在application_folder/public/controllers/original 目录下.您可以在视图中通过简单的调用(例如 @model_instance.csvdata.url)轻松访问上传的文件.例如,根据您的问题,您正在上传一个名为 example_data.csv 的文件,然后在您的视图中您可以将其用作:

With the above setup, whenever you upload a file, it will be stored under application_folder/public/controllers/original directory. And you can easily access the uploaded file with a simple call like @model_instance.csvdata.url within your view. For example, as per your question you are uploading a file named example_data.csv then in your view you can use it as:

  ## Replace @model_instance with Model instance (contains `csvdata` attachment)  
  <%= link_to "Example Data", @model_instance.csvdata.url %> 

这将生成一个指向上传文件的可点击链接:

which will generate a clickable link to the uploaded file as:

  <a href="/controllers/original/example_data.csv?12345678">Example Data</a>

注意: ?12345678 是由 rails 附加的随机种子.

NOTE: ?12345678 is a random seed appended by rails.

 :default_url => "/controllers/:style/example_data.csv"

在配置中指定 :default_url 选项总是一个好习惯.原因是如果用户在创建记录时没有上传文件,但后来尝试访问该记录的 uploaded file 然后他们将被定向到 default_url 中设置的链接.另请注意,对于此特定记录,所有 attachment 相关列都将设置为 nil.

Its always a good practice to specify :default_url option in the configuration. The reason for that is in case user doesn't upload a file while creating a record but later tries to access the uploaded file for this record then they would be directed to the link set in default_url. Also, note that for this particular record all the attachment related columns would be set to nil.

另外,我很困惑当您想在应用程序中上传相同的文件时,为什么要让 default_url 指向 example_data.csv.标准方法是使用名为 missing.csvmissing.png 的默认文件,该文件与您上传的文件无关.请记住将 missing.csvmissing.png 放在 /controllers/original/ 文件夹中,即 :default_url> 路径.否则,点击链接时您会收到错误消息.

Also, I am confused why would you want the default_url point to example_data.csv when you want to upload the same file in the application. The standard way is to have a default file named as missing.csv or missing.png which is not associated to your uploaded files. Please remember to place missing.csv or missing.png inside the /controllers/original/ folder i.e., :default_url path. Otherwise, upon clicking link you would get an error.

这篇关于回形针更改 URL/路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆