简体   繁体   English

Rails - 如何获取资产的文件路径(不是 url 路径)?

[英]Rails - How to get file path (not url path) for assets?

I have a weekly generator that zips some files.我有一个每周生成器,可以压缩一些文件。 Some are assets in app/assets/images/xxx.xxx some are uploaded via Paperclip and in public/system/env...有些是app/assets/images/xxx.xxx中的资产,有些是通过 Paperclip 和public/system/env...上传的public/system/env...

The file paths I have start after app , without /images/ or public .我在app之后开始的文件路径,没有/images/public

I'll get something like /assets/some.png or /system/production/xyz/some.jpg , and these will be served fine as a URL path, but I want to collect them in a file.我会得到类似/assets/some.png/system/production/xyz/some.jpg东西,这些可以作为 URL 路径使用,但我想将它们收集到一个文件中。

How can I get the filepath of an asset?如何获取资产的文件路径?


I think this is an easier way to explain it.我认为这是一种更简单的解释方式。

Given the asset URI string, how can I find and get the asset's file path?给定资产 URI 字符串,如何找到并获取资产的文件路径?

The asset URL path /assets/logo.png could be one of the files:资产 URL 路径/assets/logo.png可以是以下文件之一:

public/logo.png
public/assets/logo.png
app/assets/images/logo.png 

在生产中,资产应该从公共根文件夹 /public/assets/logo.png 预编译和提供

Rails.root gives you the absolute path to your project's root. Rails.root为您提供了项目根目录的绝对路径。 (It returns a Pathname object) (它返回一个Pathname对象)

You can then do something like this: 然后,您可以执行以下操作:

Rails.root.join("some_dir1", "some_dir2", "some_file")

So to get a path to a file inside public/assets : 因此,要获取public / assets中文件的路径:

Rails.root.join("public", "assets", "file.jpg")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM