Ruby on rails Rails-显示关联对象属性的列表
我有一个列出小说的页面,我想展示每个相关插图的缩略图。我知道关联正在工作,因为我可以显示每个关联插图对象的toString等价物。当我尝试遍历插图列表时,我得到:Ruby on rails Rails-显示关联对象属性的列表,ruby-on-rails,Ruby On Rails,我有一个列出小说的页面,我想展示每个相关插图的缩略图。我知道关联正在工作,因为我可以显示每个关联插图对象的toString等价物。当我尝试遍历插图列表时,我得到: undefined method `image_thumbnail_url' for #<ActiveRecord::Relation:0x007f023c07aa18> 未定义的方法“图像\u缩略图\u url”# 代码如下: <% if notice %> <p id="notice">&l
undefined method `image_thumbnail_url' for #<ActiveRecord::Relation:0x007f023c07aa18>
未定义的方法“图像\u缩略图\u url”#
代码如下:
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1 style="padding-left:25px">Archive Overview</h1>
<% @novels.each do |novel| %>
<div class="row">
<div class="span3" style="padding:0px 0px 25px 25px">
<%= link_to novel.name, novel %>
</div>
<div class="span4">
<p><%= novel.author%></p>
<p><%= novel.publisher %></p>
<p><%= novel.publication_date %></p>
</div>
<div class="span5">
<div style="display: none;"><%= illustrations = @novels.map{ |novel| novel.illustrations} %>
</div>
<ul>
<% illustrations.each do |illustration| %>
<li><%= illustration.image_thumbnail_url %></li>
<% end %>
</ul>
</div>
</div>
档案概述
您在代码中做了一些非常奇怪的事情。首先,如果您想在不显示代码的情况下运行代码,可以使用
而不是
实际问题是:
illustrations = @novels.map{ |novel| novel.illustrations }
执行此操作时,不会得到带有插图对象的数组,而是得到带有插图对象集合的数组。而且集合没有方法image\u缩略图\u url
您已经在反复阅读小说,因此我建议您只需这样做:
<div class="span5">
<ul>
<% novel.illustrations.each do |illustration| %>
<li><%= illustration.image_thumbnail_url %></li>
<% end %>
</ul>
</div>
真管用!还感谢您提供了有关排除ruby代码部分中的“=”以防止其显示的提示。