Reputation: 3527
I want to display a novel object and each of the illustrations associated with it on a web page. 1. I don't know how to properly write the code to get the associated illustrations of a novel. 2. I don't know how to properly reference an attribute of illustration objects. 3. I think my error may be from something unrelated.
Novel model:
Class Novel < ActiveRecord::Base
has_many :illustrations
attr_accessible :id, :name, :author, :edition, :publisher, :publication_city, :publication_date, :illustrations
validates :id, uniqueness: true, presence: true
validates :name, :author, :edition, :publisher, :publication_city, :publication_date, presence: true
end
Illustration model:
class Illustration < ActiveRecord::Base
belongs_to :novel
attr_accessible :id, :illustrator, :image_url,
:image_thumbnail_url, :name, :source, :tags,
:description, :novel_id
validates :id, uniqueness: true
validates :name, :source, presence: true
validates :image_url,
format: { with: %r{\.{gif|.jpg|.png}\Z}i,
message: 'must be a URL for .gif, .jpg, or .png'}
validates :image_thumbnail_url,
format: { with: %r{\.{gif|.jpg|.png}\Z}i,
message: 'must be a URL for .gif, .jpg, or .png'}
validates :illustrator, :tags, :description, presence: true
end
Novel index class:
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1 style="padding-left:25px">Archive Overview</h1>
<% @novels.each do |novel| %>
<%= illustrations = novel.map(&:illustration) %>
<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 class="span5">
<ul>
<% for illustrations.each do |illustration| %>
<li><%= illustration.name %></li>
<% end %>
</ul>
</div>
</div>
</div>
<% end %>
The error I get with any combination of for loop syntax on illustrations.each:
SyntaxError in Booklist#index
Showing /home/joe/Documents/workspace/archive/app/views/booklist/index.html.erb where line #21 raised:
/home/joe/Documents/workspace/archive/app/views/booklist/index.html.erb:21: syntax error, unexpected '\n', expecting tCOLON2 or '[' or '.'
Extracted source (around line #21):
18: <ul>
19: <% for illustrations.each do |illustration| %>
20: <li><%= illustration.name %></li>
21: <% end %>
22: </ul>
23: </div>
24: </div>
Trace of template inclusion: app/views/booklist/index.html.erb
Rails.root: /home/joe/Documents/workspace/archive
Upvotes: 0
Views: 685
Reputation: 16730
You are mixing the syntax of for
that is rarely with the each method. You should do it like this
<% illustrations.each do |illustration| %>
<li><%= illustration.name %></li>
<% end %>
Upvotes: 2