Jackson Cunningham
Jackson Cunningham

Reputation: 5073

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

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)

But right now, the url "avatars/1.jpg" is showing up as broken.

Upvotes: 0

Views: 82

Answers (1)

Jayem
Jayem

Reputation: 215

Did you try using Dir?

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.

Updated Answer:

You are using the path /assets/images/avatars/1.jpg when it should be /assets/avatars/1.jpg. The images portion should be removed when referring to the image in rails

Upvotes: 1

Related Questions