简体   繁体   English

Rails:链接到图像中的子目录

[英]Rails: Link to sub directory within images

I'm seeding some items into my app. 我正在将一些项目播种到我的应用程序中。 The item photos are kept under items sub directory like so: assets/images/items/1.jpg 物品照片将保存在items子目录下,例如: assets/images/items/1.jpg

I want to do something like: 我想做类似的事情:

all_images.each do
   item = Item.new(photo: "1.jpg")
end

So that I can reference my items in rails views like image_tag(some_item.photo) 这样我就可以在诸如image_tag(some_item.photo) rails视图中引用我的项目

But right now, the url "avatars/1.jpg" is showing up as broken. 但是现在,URL“ avatars / 1.jpg”显示为损坏。

Did you try using Dir ? 您尝试过使用Dir吗?

all_images = Dir.entries(Dir.pwd + "/app/assets/images") ? all_images = Dir.entries(Dir.pwd + "/app/assets/images")

Also it looks like you'll need the full path starting with /assets/ for each photo to show up. 同样,要显示每张照片,您都需要以/assets/开头的完整路径。

Updated Answer: 更新的答案:

You are using the path /assets/images/avatars/1.jpg when it should be /assets/avatars/1.jpg . 您在使用路径/assets/images/avatars/1.jpg时应使用/assets/avatars/1.jpg The images portion should be removed when referring to the image in rails 在导轨中引用图像时,应除去images部分

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

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