Units have many rooms. Rooms belong to units. When I show the detail
of a unit, I want to show a list of rooms belonging to the unit as well.
In the UnitController:
def show
@unit = Unit.find(params[:id])
session[:unit_id] = @unit.id
@room = Room.find(:all,
:conditions => [ "unit_id = ?", @unit[:id] ])
end
In /units/show.rhtml:
<h1>Room Info</h1>
<% unless @room.nil? %>
<ul>
<% for room in @rooms %>
<li><%= @room.name %>
<% end %>
</ul>
<% end %>
Yet I still get this exception:
NoMethodError in Units#show
You have a nil object when you didn''t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.each
Questions:
1) Why do I have a nil array?
2) Even if I have a nil array, why doesn''t <% unless @room.nil?
%> stop
it?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Because in show you are using @rooms which you haven''t set anywhere Fred -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Taylor Strait wrote:> Units have many rooms. Rooms belong to units. When I show the detail > of a unit, I want to show a list of rooms belonging to the unit as well. > In the UnitController: > > def show > @unit = Unit.find(params[:id]) > session[:unit_id] = @unit.id > @room = Room.find(:all, > :conditions => [ "unit_id = ?", @unit[:id] ]) > end > > > In /units/show.rhtml: > > <h1>Room Info</h1> > <% unless @room.nil? %> > <ul> > <% for room in @rooms %> > <li><%= @room.name %> > <% end %> > </ul> > <% end %> > > > Yet I still get this exception: > > NoMethodError in Units#show > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occured while evaluating nil.each > > > Questions: > 1) Why do I have a nil array? > 2) Even if I have a nil array, why doesn''t <% unless @room.nil? %> stop > it? > >Hey Nowhere in your action is @rooms defined... I think it should look as follows: def show @unit = Unit.find(params[:id]) session[:unit_id] = @unit.id @rooms = @unit.rooms #assuming as you say unit "has_many :rooms" end And in the view do the following: <ul> <% for room in @rooms %> <li><%=h room.name %></li> <% end %> </ul> I hope that helps man! Cheery-o Gustav Paul gustav-PUm+PnBUKx7YkQIYctQFYw@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fred: You are right. A simple typo caused most of it. Gustav: Your way is much neater! Why bust my fingers when ActiveRecord can do it for me? Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Gustav Paul <gustav@...> writes:> def show > <at> unit = Unit.find(params[:id]) > session[:unit_id] = <at> unit.id > <at> rooms = <at> unit.rooms #assuming as you say unit "has_many :rooms" > end > > And in the view do the following: > > <ul> > <% for room in <at> rooms %> > <li><%=h room.name %></li> > <% end %> > </ul>No need to even set an instance variable <ul> <% for room in @unit.rooms %> <li><%=h room.name %></li> <% end %> </ul> Gareth --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---