Step 17 undefined method


I have an "undefined method `each' for Track:0x0000000413b830" error when I click on an album, the error is highlighting

 <% @tracks.each do |track| %>

which is part of my apps/views/albums/show.html.erb file:

<div class="show row">
  <div class="cover col-md-4"><img src="<%= @album.cover %>"></div>

  <div class="col-sm-8">
    <h3 class="album-title"><%= @album.title %></h3>
    <p class="album-artist"><%= @album.artist %></p>

    <% @tracks.each do |track| %>
    <div class="track row">
      <div class="name col-md-8"><%= %></div>
      <div class="minutes col-md-4"><%= track.minutes %>min</div>
    <% end %>

Albums controller:

class AlbumsController < ApplicationController
  def index
    @albums = Album.all
  def show
  	@album = Album.find(params[:id])
    @tracks = Track.find(params[:id])

Tracks migration:

class CreateTracks < ActiveRecord::Migration
  def change
    create_table :tracks do |t|
t.string :name
      t.string :minutes
      t.references :album

Any idea what the bug is?


Hi @arrayblaster08299,

You need to select the tracks that belong to that album, rather than just the track with the same ID as whatever album you're currently viewing. Try this instead:

@tracks = @album.tracks


that worked! thanks!

I'm confused though because in another topic thread HERE that line was suggested by you to be this instead:

@tracks = Track.find(params[:id])

Is this because this Rails tutorial somehow changed or???? just confused


@arrayblaster08299 I think that was just a simple mistake I made, I'd go fix my post but it's pretty old now and the OP seems to have vanished.

I just checked against my code, and the answer I gave here to you is the correct one :slight_smile:

Thanks for checking through other posts before asking! Not that many people do. (Also, sorry for the confusion)


Awesome, no worries! I thought I'd double check.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.