ruby on rails - NoMethodError in Songs#genre . Undefined method `each' for nil:NilClass -


i'm struggling in ruby on rails application.
model:

class song < activerecord::base    attr_accessible :duration, :title, :track_url, :genre, :image_url  end 

i have made new method in songs controller:

def genre     @songs = song.find_all_by_genre(params[:id])     @genre = (params[:id])      respond_to |format|       format.html # index.html.erb       format.json {render json: @songs}     end   end 

this partial view shows dynamically genres , makes them select-able:

<% @songs = song.select('distinct genre')%> <li>     <% @songs.each |g| %>         <a href="/genre/<%= g.genre %>"><%= g.genre %></a>     <% end %> </li> 

till here working , when select genre instead rendering songs in songs/genre.html.erb view gives me error can not understand have method in controller

nomethoderror in songs#genre  showing c:/sites/oml/app/views/songs/genre.html.erb line #3 raised:  undefined method `each' nil:nilclass extracted source (around line #3):  1: <h2><%= @genre %></h2> 2:  3: <% @songs.each |song| %> 4:     <%= song.title %> 5:     <%= song.genre %> 6: <% end %> 

i have tried same way in brand new application , working!! problem mine ?

change partial check if there songs, so

<li>   <% if @songs %>     <% @songs.each |g| %>       <a href="/genre/<%= g.genre %>"><%= g.genre %></a>     <% end %>   <% else %>     no songs   <% end %> </li> 

also change genre.html.erb to

<% if @songs %>   <% @songs.each |song| %>     <%= song.title %>     <%= song.genre %>   <% end %> <% else %>   no songs <% end %> 

Comments