简体   繁体   English

如何将种子图像数据传递到Rails视图中?

[英]How to pass seeded image data into Rails views?

I seeded my Rails db with the following for Posts: title, description, body, and image. 我在Rails db的种子中添加了以下内容:标题,描述,正文和图像。 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')%>. <%= asset_path('post.post_image')%>。 Nothing appears 什么都没出现
  • <%= asset_url('post.post_image')%>. <%= asset_url('post.post_image')%>。 I get http://localhost:3000/post.main.image.jpg 我得到了http:// localhost:3000 / post.main.image.jpg
  • <%= image_tag('post.post_image')%>. <%= image_tag('post.post_image')%>。 I get a small icon with Post next to it but not a full image. 我看到旁边有一个小图标,但没有完整图像。
  • <%= image_tag @post_image%>. <%= image_tag @ post_image%>。 I get a small icon but not a full image. 我得到一个小图标,但没有完整图像。
  • <%= image_alt(post.post_image) %>. <%= image_alt(post.post_image)%>。 I get at main.image.jpg present 我现在得到main.image.jpg

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. Image_tag将命中资产管道下的图像。 Did a db:reset after I fixed the seed file to just be: post_image: 'main.image.jpg'. 在将种子文件固定为:post_image:'main.image.jpg'之后,是否执行了db:reset。 Correct code is: 正确的代码是:

<%= image_tag post.post_image %>

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

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