Paperclip :url 不创建路由 [英] Paperclip :url does not create Routes

查看:56
本文介绍了Paperclip :url 不创建路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回形针中使用 :url 选项有什么意义?:path 选项实际上会更改文件的保存位置,但 :url 选项似乎没有做任何事情.它仅在指向可公开访问的文件位置时才有效.那时,任何人都可以访问该 url.如果我更改 url 使其与路径不匹配,则它不起作用.据我所知,它不会以任何方式创建任何路线.有什么我在这里想念的吗.这个选项有什么意义?让某人指定 :url 而不实际创建路由似乎过于混乱.

What is the point of using the :url option with paperclip? The :path option does in fact change the location where the file is saved, but the :url option doesn't seem to do a thing. It only works when it points to a publicly accessible file location. At that point, the url is already accessible to anyone. If I change the url so that it doesn't match the path, it does not work. As far as I can tell, it does not create any routes either way. Is there something I am missing here. What is the point of this option? It seems overly confusing to let someone specify a :url without actually creating a route.

推荐答案

I found 这篇文章 有助于理解 :path:url 之间的区别.

I found this post useful in understanding the difference between :path and :url.

  • :path 设置应用程序中存储文件的目录.
  • :url 设置用户可以用来访问图像的 url.
  • :path sets the directory in your application where the file is stored.
  • :url sets the url that users can use to access the image.

您说得对,回形针不会为您创建路线.但是,:url 选项确实让您能够选择您的用户可以使用哪个(现有)路由来下载特定图像.

You are right, paperclip does not create a route for you. However, the :url option does give you the ability to select which (existing) route your users can use to download a specific image.

:path:url 通常是齐头并进的.如果你坚持使用回形针 :default_url 路径已经为你配置好了.只需点击网址,一切都会正常工作.

:path and :url usually go hand in hand. If you stick to the paperclip :default_url the path is already configured for you. Just hit the url and everything will work fine.

更改文件位置

在这个例子中,我渲染了一个用户头像:

In this example I am rendering a users avatar:

<%= image_tag @user.avatar.url %>

现在,假设您想更改存储图像的位置,您可以将以下代码添加到您的模型中:

Now, lets say that you wanted to change the location that images are stored, you can add the following code to your model:

  has_attached_file :avatar,
    :path => "public/system/:class/:id/:filename"

但是,图像将无法成功渲染.这是因为存储图像的新路径与 :default_url 不匹配.因此,您还需要指定一个新网址:

However, the image will not render successfully. This is because the new path, where your images are stored, does not match the :default_url. Therefore, you will also need to specify a new url:

  has_attached_file :avatar,
    :path => "public/system/:class/:id/:filename"
    :url => "/system/:class/:id/:basename.:extension"

现在图像 url 与文件存储在您的服务器上的位置相匹配,并且图像渲染成功.

Now the image url matches the location that the file is stored on your server and the image renders successfully.

路径与网址

总而言之,:url 告诉回形针在服务器上查找图像的位置.:path 告诉回形针在创建或更新记录时上传图像的位置.

To summarize, :url tells paperclip where abouts on the server to look for an image. :path tells paperclip where to upload an image, when creating or updating a record.

:path:url 都应该指向同一个位置,才能成功渲染图像.

Both :path and :url should point to the same location, in order to render an image successfully.

这篇关于Paperclip :url 不创建路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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