当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails - Image not showing in view

问题描述:

I'm currently trying to iterate over my entries and display the image from each of them. The image url is stored as a string in the database.

<% @feed.entries.each do |image| %>

<div class="grid-item">

<%= image_tag(image) %>

</div>

<% end %>

When using this, unfortunately this comes up within the image src

/images/#<Entry:0x007fcf40144210>

Not too sure what this is doing.

EDIT

#<Entry id: 1, title: nil, image: "http://40.media.tumblr.com/889c65a662a1b690f299593...", source: nil, created_at: "2016-04-11 11:31:00", updated_at: "2016-04-11 11:31:00", feed_id: 1>, #<Entry id: 2, title: nil, image: "http://41.media.tumblr.com/3cfd8c9c93870fa716356bc...", source: nil, created_at: "2016-04-11 11:31:00", updated_at: "2016-04-11 11:31:00", feed_id: 1>, #<Entry id: 3, title: nil, image: "http://40.media.tumblr.com/7906907023dd04ff5c4be15...", source: nil, created_at: "2016-04-11 11:31:00", updated_at: "2016-04-11 11:31:00", feed_id: 1>, #<Entry id: 4, title: nil, image: "http://49.media.tumblr.com/3405440b713440ba490f473...", source: nil, created_at: "2016-04-11 11:31:00", updated_at: "2016-04-11 11:31:00", feed_id: 1>, #<Entry id: 5, title: nil, image: "http://45.media.tumblr.com/9185ff46269e01bd6d7827e...", source: nil, created_at: "2016-04-11 11:31:00", updated_at: "2016-04-11 11:31:00", feed_id: 1>, #<Entry id: 6, title: nil, image: "http://41.media.tumblr.com/e1d5e279df2b765c901c9fa...", source: nil, created_at: "2016-04-11 11:31:00", updated_at: "2016-04-11 11:31:00", feed_id: 1>, #<Entry id: 7, title: nil, image: "http://41.media.tumblr.com/061ab14be3316e82b1ea67e...", source: nil, created_at: "2016-04-11 11:31:00", updated_at: "2016-04-11 11:31:00", feed_id: 1>, #<Entry id: 8, title: nil, image: "http://41.media.tumblr.com/975c58b368d20d6f54f3eed...", source: nil, created_at: "2016-04-11 11:31:00", updated_at: "2016-04-11 11:31:00", feed_id: 1>, #<Entry id: 9, title: nil, image: "http://49.media.tumblr.com/65e811cefd6ec3235d933a0...", source: nil, created_at: "2016-04-11 11:31:00", updated_at: "2016-04-11 11:31:00", feed_id: 1>, #<Entry id: 10, title: nil, image: "http://41.media.tumblr.com/b2486d84cc2c30bbdb50b0b...", source: nil, created_at: "2016-04-11 11:31:00", updated_at: "2016-04-11 11:31:00", feed_id: 1>, ...]>

网友答案:

You are iterating through entries, so ideally your iterator variable should be named as entry instead of image which makes the confusion here. And, every entry has an image, so it should look like the followings:

<% @feed.entries.each do |entry| %>
  <div class="grid-item">
   <%= image_tag(entry.image) %>
  </div>
<% end %>

Hope it makes sense!

网友答案:

@feed.entries is evidently not an array of image urls.

It seems that Entry is an object. Ostensibly one of it's columns is the url.

Is there an entry.url or entry.image_url. column?

In which case you could use @feed.entries.pluck(:image).each to operate over an array of urls.

分享给朋友:
您可能感兴趣的文章:
随机阅读: