简体   繁体   中英

How to pass seeded image data into Rails views?

I seeded my Rails db with the following for Posts: title, description, body, and image. For image I used data in the assets. So for example in the seed file I have:

post_image: '/images/main.image.jpg'

I'm able to hit all the other data in the views doing the typical:

<%= post.title %>

How do I pull in the image? I've tried the following to no avail:

  • <%= asset_path('post.post_image')%>. Nothing appears
  • <%= asset_url('post.post_image')%>. I get http://localhost:3000/post.main.image.jpg
  • <%= image_tag('post.post_image')%>. I get a small icon with Post next to it but not a full image.
  • <%= image_tag @post_image%>. I get a small icon but not a full image.
  • <%= image_alt(post.post_image) %>. I get at main.image.jpg present

Did I seed incorrectly or am I just using the wrong helper to get the image?

Yeah I seeded incorrectly. Image_tag will hit images under the asset pipeline. Did a db:reset after I fixed the seed file to just be: post_image: 'main.image.jpg'. Correct code is:

<%= image_tag post.post_image %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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