Hello all,
I''m having trouble passing a collection of items to be rendered by a
partial. The partial is displaying line items from a shopping cart. I
must be missing something really simple, because I can''t figure this
one out! Here is my relevant code:
***The view in question: app/views/order/show_cart.rhtml
<h1>Your Cart</h1>
...
<tr><th>Event</th><th>Unit
Price</th><th>Qty</th><th>Ext. Price</th></
tr>
<%= render(:partial => ''line_item'', :collection =>
@order.line_items.map {|key, val| val} ) %>
...
I have to pre-process the rows (@order.line_items) because I
aggregate some rows together from the order before presenting them to
the view. (It''s an event ticketing app, and multiple seats for the
same event in one order get aggregated into one line_item
with :quantity > 1). I only need the values of the hash (the sub-
hashes below with symbols for keys).
***Sample @order.line_items.inspect:
{
26=>{:quantity=>1, :price=>0.0, :ext_price=>0.0,
:name=>"Event A"},
27=>{:quantity=>1, :price=>0.0, :ext_price=>0.0,
:name=>"Event B"}
}
***The partial: app/views/order/_line_item.rhtml:
<tr>
<td><%=h line_item[:name] %></td>
<td><%=h number_to_currency(line_item[:price]) %></td>
<td><%=h line_item[:quantity] %></td>
<td><%=h number_to_currency(line_item[:ext_price]) %></td>
</tr>
It seems that when the partial is rendered, it chokes on the first
ERb statement in the partial (line_item[:name]). line_item appears to
be nil in the partial, and of course doesn''t have a [] method. But
why?? :-)
If anyone has ideas on this (or on how to troubleshoot further), I
would really appreciate hearing them!
Thanks,
Brad Ediger
--
Brad Ediger
866-EDIGERS
Try passing @order.line_items.collect { |k,v| v } as the collection.
Inside the partial, "<% p line_item -%>" should verify that you
are
receiving a LineItem object.
Also, assuming that''s an ActiveRecord object,
"line_item.quantity"
seems to be the preferred method these days, instead of
line_item[:quantity]
It might be helpful to move the collect (or map) call outside of the
render. That would give you a place to "p" or "inspect" it
before it
enters the lair of the beast.
That being said, it''s been a long day, and I didn''t actually
run any
code before writing this message. Heh.
--Wilson.
On 9/5/05, Brad Ediger <brad-sod+mMc99o6+XT7JhA+gdA@public.gmane.org>
wrote:> Hello all,
> I''m having trouble passing a collection of items to be rendered by
a
> partial. The partial is displaying line items from a shopping cart. I
> must be missing something really simple, because I can''t figure
this
> one out! Here is my relevant code:
>
> ***The view in question: app/views/order/show_cart.rhtml
> <h1>Your Cart</h1>
>
> ...
> <tr><th>Event</th><th>Unit
Price</th><th>Qty</th><th>Ext. Price</th></
> tr>
> <%= render(:partial => ''line_item'', :collection
=>
> @order.line_items.map {|key, val| val} ) %>
> ...
>
> I have to pre-process the rows (@order.line_items) because I
> aggregate some rows together from the order before presenting them to
> the view. (It''s an event ticketing app, and multiple seats for the
> same event in one order get aggregated into one line_item
> with :quantity > 1). I only need the values of the hash (the sub-
> hashes below with symbols for keys).
>
> ***Sample @order.line_items.inspect:
> {
> 26=>{:quantity=>1, :price=>0.0, :ext_price=>0.0,
:name=>"Event A"},
> 27=>{:quantity=>1, :price=>0.0, :ext_price=>0.0,
:name=>"Event B"}
> }
>
> ***The partial: app/views/order/_line_item.rhtml:
> <tr>
> <td><%=h line_item[:name] %></td>
> <td><%=h number_to_currency(line_item[:price]) %></td>
> <td><%=h line_item[:quantity] %></td>
> <td><%=h number_to_currency(line_item[:ext_price])
%></td>
> </tr>
>
> It seems that when the partial is rendered, it chokes on the first
> ERb statement in the partial (line_item[:name]). line_item appears to
> be nil in the partial, and of course doesn''t have a [] method. But
> why?? :-)
>
> If anyone has ideas on this (or on how to troubleshoot further), I
> would really appreciate hearing them!
>
This seems really weird. To clear up some confusion -- LineItem isn''t
an ActiveRecord object. It is a Hash of properties that I assembled
ad-hoc to meet the needs of this view. You''re absolutely right,
Wilson -- the map call fits better in the controller. But this object
doesn''t seem to be getting from the controller to the view. There has
to be something I''m missing. Let the code tell it: (I''ve
bolded the
lines of code that follow the object from the controller to the view.)
***app/controllers/order_controller.rb
class OrderController < ApplicationController
def show_cart
if session[:cart].nil?
@order = nil
@line_items = nil
else
@order = Order.find(session[:cart])
@line_items = @order.line_items.collect { |k,v| v }
puts "Line items: "
puts @line_items.inspect # for console debugging
end
end
end
***app/views/order/show_cart.rhtml
<h1>Your Cart</h1>
<% if @order.nil? || @order.reservations == [] %>
You have no items in your cart. <%= link_to(''Find
Events'', :controller => ''events'', :action =>
''list'') %>
<% else %>
<table>
<tr><th>Event</th><th>Unit
Price</th><th>Qty</th><th>Ext. Price</th></
tr>
<%= render(:partial => ''line_item'', :collection =>
@line_items ) %>
</table>
<br />
TODO: checkout, etc.
<% end %>
***app/views/order/_line_item.rhtml
<tr>
<td><%=h line_item.inspect %></td>
</tr>
***app/models/order.rb (just for reference)
class Order < ActiveRecord::Base
belongs_to :customer
has_many :reservations
def line_items
# indexed by seat group (OK as key because sg uniquely
identifies event)
# list == {26 => {:name => ''Christmas Glo'',
:quantity =>
3, :price => 10.0, :ext_price => 30.0}, ...}
list = {}
reservations.each do |r|
if list.has_key? r.seat_group.id
list[r.seat_group.id][:quantity] = list[r.seat_group.id]
[:quantity] + 1
list[r.seat_group.id][:ext_price] = list[r.seat_group.id]
[:ext_price] + list[r.seat_group.id][:price]
else
list[r.seat_group.id] = {
:name => r.event.name,
:quantity => 1,
:price => r.seat_group.price,
:ext_price => r.seat_group.price,
:seat_group => r.seat_group, # TODO: need this?
:event => r.seat_group.event # TODO: need this?
}
end
end
list
end
end
What''s really weird is that I get this output at the console (from
the controller):
Line items:
[{:quantity=>1, :price=>0.0, :ext_price=>0.0,
:seat_group=>#<SeatGroup:0
x23edb10 @attributes={"event_id"=>"6",
"name"=>"Right Section",
"price"=>"0", "venue_id"=>"5",
"id"=>"28",
"number_of_seats"=>"500"},
@event=#<Event:0x23db94c
@attributes={"name"=>"Easter",
"event_at"=>"2006-03-23 19:00:00",
"ticket_sale_ends_at"=>"2006-03-23
19:00:00", "event_appears_at"=>"2005-09-03
10:51:00",
"venue_id"=>"5",
"coordinator_user_id"=>"0",
"id"=>"6",
"ticket_sale_begins_at"=>"2005-09-03 10:51:00",
"position"=>"0"}
>>, :event=>#<Event:0x23db94c
@attributes={"name"=>"Easter",
"event_at"=>"2006-03-23 19:00:00",
"ticket_sale_ends_at"=>"2006-03-23
19:00:00", "event_appears_at"=>"2005-09-03
10:51:00",
"venue_id"=>"5",
"coordinator_user_id"=>"0",
"id"=>"6",
"ticket_sale_begins_at"=>"2005-09-03 10:51:00",
"position"=>"0"}
>, :name=>"Easter"}]
but I get ''nil'' as the sole output from the view. I''m
not sure if
this whole approach is the best way to do it. Most of the data the
view is displaying here is created on-the-fly as an aggregate of the
rows in the model. A lot of this info is virtual. I just figured it
would be easier to have the model create the data, glue it together
in a Hash, and have the view take it apart. Are there any better ways
to do this?
Thanks so much for all your help!
Brad
--
Brad Ediger
866-EDIGERS
On Sep 5, 2005, at 9:19 PM, Wilson Bilkovich wrote:
> Try passing @order.line_items.collect { |k,v| v } as the collection.
> Inside the partial, "<% p line_item -%>" should verify that
you are
> receiving a LineItem object.
> Also, assuming that''s an ActiveRecord object,
"line_item.quantity"
> seems to be the preferred method these days, instead of
> line_item[:quantity]
> It might be helpful to move the collect (or map) call outside of the
> render. That would give you a place to "p" or "inspect"
it before it
> enters the lair of the beast.
>
> That being said, it''s been a long day, and I didn''t
actually run any
> code before writing this message. Heh.
>
> --Wilson.
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Sorry for the short reply to your content-filled post; What happens if you choose a different name for the collection than "@line_items", such as "@test_somethings"? I don''t have the source in front of me at the moment, but I recall there being some "automagic" thing regarding collections with the pluralized name of a partial. I pray that this doesn''t fix your problem, because that would be too crazy to believe.. but it might be worth a try. Also, just to make sure we''re not missing something simple, try adding: <% p @line_items -%> in the main view, right before the render(:partial) call. --Wilson. On 9/5/05, Brad Ediger <brad-sod+mMc99o6+XT7JhA+gdA@public.gmane.org> wrote:> This seems really weird. To clear up some confusion -- LineItem isn''t an > ActiveRecord object. It is a Hash of properties that I assembled ad-hoc to > meet the needs of this view. You''re absolutely right, Wilson -- the map call > fits better in the controller. But this object doesn''t seem to be getting > from the controller to the view. There has to be something I''m missing. Let > the code tell it: (I''ve bolded the lines of code that follow the object from > the controller to the view.) > > ***app/controllers/order_controller.rb > class OrderController < ApplicationController > def show_cart > if session[:cart].nil? > @order = nil > @line_items = nil > else > @order = Order.find(session[:cart]) > @line_items = @order.line_items.collect { |k,v| v } > puts "Line items: " > puts @line_items.inspect # for console debugging > end > end > end > > ***app/views/order/show_cart.rhtml > <h1>Your Cart</h1> > > <% if @order.nil? || @order.reservations == [] %> > You have no items in your cart. <%= link_to(''Find Events'', :controller => > ''events'', :action => ''list'') %> > <% else %> > <table> > <tr><th>Event</th><th>Unit Price</th><th>Qty</th><th>Ext. Price</th></tr> > <%= render(:partial => ''line_item'', :collection => @line_items ) %> > </table> > <br /> > TODO: checkout, etc. > <% end %> > > > ***app/views/order/_line_item.rhtml > <tr> > <td><%=h line_item.inspect %></td> > </tr> > > ***app/models/order.rb (just for reference) > class Order < ActiveRecord::Base > belongs_to :customer > has_many :reservations > > def line_items > # indexed by seat group (OK as key because sg uniquely identifies event) > # list == {26 => {:name => ''Christmas Glo'', :quantity => 3, :price => > 10.0, :ext_price => 30.0}, ...} > list = {} > reservations.each do |r| > if list.has_key? r.seat_group.id > list[r.seat_group.id][:quantity] = list[r.seat_group.id][:quantity] > + 1 > list[r.seat_group.id][:ext_price] > list[r.seat_group.id][:ext_price] + list[r.seat_group.id][:price] > else > list[r.seat_group.id] = { > :name => r.event.name, > :quantity => 1, > :price => r.seat_group.price, > :ext_price => r.seat_group.price, > :seat_group => r.seat_group, # TODO: need this? > :event => r.seat_group.event # TODO: need this? > } > end > end > list > end > end > > > > What''s really weird is that I get this output at the console (from the > controller): > > Line items: > [{:quantity=>1, :price=>0.0, :ext_price=>0.0, > :seat_group=>#<SeatGroup:0x23edb10 > @attributes={"event_id"=>"6", "name"=>"Right Section", "price"=>"0", > "venue_id"=>"5", "id"=>"28", "number_of_seats"=>"500"}, > @event=#<Event:0x23db94c @attributes={"name"=>"Easter", > "event_at"=>"2006-03-23 19:00:00", > "ticket_sale_ends_at"=>"2006-03-23 19:00:00", > "event_appears_at"=>"2005-09-03 10:51:00", "venue_id"=>"5", > "coordinator_user_id"=>"0", "id"=>"6", > "ticket_sale_begins_at"=>"2005-09-03 10:51:00", > "position"=>"0"}>>, :event=>#<Event:0x23db94c @attributes={"name"=>"Easter", > "event_at"=>"2006-03-23 19:00:00", > "ticket_sale_ends_at"=>"2006-03-23 19:00:00", > "event_appears_at"=>"2005-09-03 10:51:00", "venue_id"=>"5", > "coordinator_user_id"=>"0", "id"=>"6", > "ticket_sale_begins_at"=>"2005-09-03 10:51:00", > "position"=>"0"}>, :name=>"Easter"}] > > but I get ''nil'' as the sole output from the view. I''m not sure if this whole > approach is the best way to do it. Most of the data the view is displaying > here is created on-the-fly as an aggregate of the rows in the model. A lot > of this info is virtual. I just figured it would be easier to have the model > create the data, glue it together in a Hash, and have the view take it > apart. Are there any better ways to do this? > > Thanks so much for all your help! > > Brad > > -- > Brad Ediger > 866-EDIGERS > > > On Sep 5, 2005, at 9:19 PM, Wilson Bilkovich wrote: > > Try passing @order.line_items.collect { |k,v| v } as the collection. > Inside the partial, "<% p line_item -%>" should verify that you are > receiving a LineItem object. > Also, assuming that''s an ActiveRecord object, "line_item.quantity" > seems to be the preferred method these days, instead of > line_item[:quantity] > It might be helpful to move the collect (or map) call outside of the > render. That would give you a place to "p" or "inspect" it before it > enters the lair of the beast. > > That being said, it''s been a long day, and I didn''t actually run any > code before writing this message. Heh. > > --Wilson. > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Well, no luck here. I changed @line_items to @items and the same
thing occurs. I added a <% p @items %> just before the render
(:partial...) and it gives the correct object on the console:
[{:quantity=>2, :price=>0.0, :ext_price=>0.0,
:seat_group=>#<SeatGroup:0
x242f344 @attributes={"event_id"=>"6",
"name"=>"Right Section",
"price"=>"0", "venue_id"=>"5",
"id"=>"28",
"number_of_seats"=>"500"},
@event=#<Event:0x24215dc
@attributes={"name"=>"Easter",
"event_at"=>"2006-03-23 19:00:00",
"ticket_sale_ends_at"=>"2006-03-23
19:00:00", "event_appears_at"=>"2005-09-03
10:51:00",
"venue_id"=>"5",
"coordinator_user_id"=>"0",
"id"=>"6",
"ticket_sale_begins_at"=>"2005-09-03 10:51:00",
"position"=>"0"}
>>, :event=>#<Event:0x24215dc
@attributes={"name"=>"Easter",
"event_at"=>"2006-03-23 19:00:00",
"ticket_sale_ends_at"=>"2006-03-23
19:00:00", "event_appears_at"=>"2005-09-03
10:51:00",
"venue_id"=>"5",
"coordinator_user_id"=>"0",
"id"=>"6",
"ticket_sale_begins_at"=>"2005-09-03 10:51:00",
"position"=>"0"}
>, :name=>"Easter"}]
I don''t understand!!
--
Brad Ediger
866-EDIGERS
On Sep 6, 2005, at 4:16 PM, Wilson Bilkovich wrote:
> Sorry for the short reply to your content-filled post;
> What happens if you choose a different name for the collection than
> "@line_items", such as "@test_somethings"?
> I don''t have the source in front of me at the moment, but I recall
> there being some "automagic" thing regarding collections with the
> pluralized name of a partial.
> I pray that this doesn''t fix your problem, because that would be
too
> crazy to believe.. but it might be worth a try.
>
> Also, just to make sure we''re not missing something simple, try
> adding:
> <% p @line_items -%> in the main view, right before the render
> (:partial) call.
>
> --Wilson.
>
> On 9/5/05, Brad Ediger <brad-sod+mMc99o6+XT7JhA+gdA@public.gmane.org>
wrote:
>
>> This seems really weird. To clear up some confusion -- LineItem
>> isn''t an
>> ActiveRecord object. It is a Hash of properties that I assembled
>> ad-hoc to
>> meet the needs of this view. You''re absolutely right, Wilson
--
>> the map call
>> fits better in the controller. But this object doesn''t seem to
be
>> getting
>> from the controller to the view. There has to be something I''m
>> missing. Let
>> the code tell it: (I''ve bolded the lines of code that follow
the
>> object from
>> the controller to the view.)
>>
>> ***app/controllers/order_controller.rb
>> class OrderController < ApplicationController
>> def show_cart
>> if session[:cart].nil?
>> @order = nil
>> @line_items = nil
>> else
>> @order = Order.find(session[:cart])
>> @line_items = @order.line_items.collect { |k,v| v }
>> puts "Line items: "
>> puts @line_items.inspect # for console debugging
>> end
>> end
>> end
>>
>> ***app/views/order/show_cart.rhtml
>> <h1>Your Cart</h1>
>>
>> <% if @order.nil? || @order.reservations == [] %>
>> You have no items in your cart. <%= link_to(''Find
>> Events'', :controller =>
>> ''events'', :action => ''list'')
%>
>> <% else %>
>> <table>
>> <tr><th>Event</th><th>Unit
Price</th><th>Qty</th><th>Ext. Price</
>> th></tr>
>> <%= render(:partial => ''line_item'', :collection
=> @line_items ) %>
>> </table>
>> <br />
>> TODO: checkout, etc.
>> <% end %>
>>
>>
>> ***app/views/order/_line_item.rhtml
>> <tr>
>> <td><%=h line_item.inspect %></td>
>> </tr>
>>
>> ***app/models/order.rb (just for reference)
>> class Order < ActiveRecord::Base
>> belongs_to :customer
>> has_many :reservations
>>
>> def line_items
>> # indexed by seat group (OK as key because sg uniquely
>> identifies event)
>> # list == {26 => {:name => ''Christmas Glo'',
:quantity =>
>> 3, :price =>
>> 10.0, :ext_price => 30.0}, ...}
>> list = {}
>> reservations.each do |r|
>> if list.has_key? r.seat_group.id
>> list[r.seat_group.id][:quantity] = list[r.seat_group.id]
>> [:quantity]
>> + 1
>> list[r.seat_group.id][:ext_price] >>
list[r.seat_group.id][:ext_price] + list[r.seat_group.id][:price]
>> else
>> list[r.seat_group.id] = {
>> :name => r.event.name,
>> :quantity => 1,
>> :price => r.seat_group.price,
>> :ext_price => r.seat_group.price,
>> :seat_group => r.seat_group, # TODO: need this?
>> :event => r.seat_group.event # TODO: need this?
>> }
>> end
>> end
>> list
>> end
>> end
>>
>>
>>
>> What''s really weird is that I get this output at the console
(from
>> the
>> controller):
>>
>> Line items:
>> [{:quantity=>1, :price=>0.0, :ext_price=>0.0,
>> :seat_group=>#<SeatGroup:0x23edb10
>> @attributes={"event_id"=>"6",
"name"=>"Right Section",
"price"=>"0",
>> "venue_id"=>"5",
"id"=>"28",
"number_of_seats"=>"500"},
>> @event=#<Event:0x23db94c
@attributes={"name"=>"Easter",
>> "event_at"=>"2006-03-23 19:00:00",
>> "ticket_sale_ends_at"=>"2006-03-23 19:00:00",
>> "event_appears_at"=>"2005-09-03 10:51:00",
"venue_id"=>"5",
>> "coordinator_user_id"=>"0",
"id"=>"6",
>> "ticket_sale_begins_at"=>"2005-09-03 10:51:00",
>> "position"=>"0"}>>,
:event=>#<Event:0x23db94c @attributes=
>> {"name"=>"Easter",
>> "event_at"=>"2006-03-23 19:00:00",
>> "ticket_sale_ends_at"=>"2006-03-23 19:00:00",
>> "event_appears_at"=>"2005-09-03 10:51:00",
"venue_id"=>"5",
>> "coordinator_user_id"=>"0",
"id"=>"6",
>> "ticket_sale_begins_at"=>"2005-09-03 10:51:00",
>> "position"=>"0"}>,
:name=>"Easter"}]
>>
>> but I get ''nil'' as the sole output from the view.
I''m not sure if
>> this whole
>> approach is the best way to do it. Most of the data the view is
>> displaying
>> here is created on-the-fly as an aggregate of the rows in the
>> model. A lot
>> of this info is virtual. I just figured it would be easier to have
>> the model
>> create the data, glue it together in a Hash, and have the view
>> take it
>> apart. Are there any better ways to do this?
>>
>> Thanks so much for all your help!
>>
>> Brad
>>
>> --
>> Brad Ediger
>> 866-EDIGERS
>>
>>
>> On Sep 5, 2005, at 9:19 PM, Wilson Bilkovich wrote:
>>
>> Try passing @order.line_items.collect { |k,v| v } as the collection.
>> Inside the partial, "<% p line_item -%>" should verify
that you are
>> receiving a LineItem object.
>> Also, assuming that''s an ActiveRecord object,
"line_item.quantity"
>> seems to be the preferred method these days, instead of
>> line_item[:quantity]
>> It might be helpful to move the collect (or map) call outside of the
>> render. That would give you a place to "p" or
"inspect" it before it
>> enters the lair of the beast.
>>
>> That being said, it''s been a long day, and I didn''t
actually run any
>> code before writing this message. Heh.
>>
>> --Wilson.
>>
>>
>> _______________________________________________
>> Rails mailing list
>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
>>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
That''s totally bizarre. Your code looks exactly like some I have in one of my projects: <%= render(:partial => ''member'', :collection => @members) %> _member.rhtml: <% @member = member %> <% p member -%> etc etc etc On 9/6/05, Brad Ediger <brad-sod+mMc99o6+XT7JhA+gdA@public.gmane.org> wrote:> Well, no luck here. I changed @line_items to @items and the same > thing occurs. I added a <% p @items %> just before the render > (:partial...) and it gives the correct object on the console: > > [{:quantity=>2, :price=>0.0, :ext_price=>0.0, :seat_group=>#<SeatGroup:0 > x242f344 @attributes={"event_id"=>"6", "name"=>"Right Section", > "price"=>"0", "venue_id"=>"5", "id"=>"28", "number_of_seats"=>"500"}, > @event=#<Event:0x24215dc @attributes={"name"=>"Easter", > "event_at"=>"2006-03-23 19:00:00", "ticket_sale_ends_at"=>"2006-03-23 > 19:00:00", "event_appears_at"=>"2005-09-03 10:51:00", > "venue_id"=>"5", "coordinator_user_id"=>"0", "id"=>"6", > "ticket_sale_begins_at"=>"2005-09-03 10:51:00", "position"=>"0"} > >>, :event=>#<Event:0x24215dc @attributes={"name"=>"Easter", > "event_at"=>"2006-03-23 19:00:00", "ticket_sale_ends_at"=>"2006-03-23 > 19:00:00", "event_appears_at"=>"2005-09-03 10:51:00", > "venue_id"=>"5", "coordinator_user_id"=>"0", "id"=>"6", > "ticket_sale_begins_at"=>"2005-09-03 10:51:00", "position"=>"0"} > >, :name=>"Easter"}] > > I don''t understand!! > -- > Brad Ediger > 866-EDIGERS > > > On Sep 6, 2005, at 4:16 PM, Wilson Bilkovich wrote: > > > Sorry for the short reply to your content-filled post; > > What happens if you choose a different name for the collection than > > "@line_items", such as "@test_somethings"? > > I don''t have the source in front of me at the moment, but I recall > > there being some "automagic" thing regarding collections with the > > pluralized name of a partial. > > I pray that this doesn''t fix your problem, because that would be too > > crazy to believe.. but it might be worth a try. > > > > Also, just to make sure we''re not missing something simple, try > > adding: > > <% p @line_items -%> in the main view, right before the render > > (:partial) call. > > > > --Wilson. > > > > On 9/5/05, Brad Ediger <brad-sod+mMc99o6+XT7JhA+gdA@public.gmane.org> wrote: > > > >> This seems really weird. To clear up some confusion -- LineItem > >> isn''t an > >> ActiveRecord object. It is a Hash of properties that I assembled > >> ad-hoc to > >> meet the needs of this view. You''re absolutely right, Wilson -- > >> the map call > >> fits better in the controller. But this object doesn''t seem to be > >> getting > >> from the controller to the view. There has to be something I''m > >> missing. Let > >> the code tell it: (I''ve bolded the lines of code that follow the > >> object from > >> the controller to the view.) > >> > >> ***app/controllers/order_controller.rb > >> class OrderController < ApplicationController > >> def show_cart > >> if session[:cart].nil? > >> @order = nil > >> @line_items = nil > >> else > >> @order = Order.find(session[:cart]) > >> @line_items = @order.line_items.collect { |k,v| v } > >> puts "Line items: " > >> puts @line_items.inspect # for console debugging > >> end > >> end > >> end > >> > >> ***app/views/order/show_cart.rhtml > >> <h1>Your Cart</h1> > >> > >> <% if @order.nil? || @order.reservations == [] %> > >> You have no items in your cart. <%= link_to(''Find > >> Events'', :controller => > >> ''events'', :action => ''list'') %> > >> <% else %> > >> <table> > >> <tr><th>Event</th><th>Unit Price</th><th>Qty</th><th>Ext. Price</ > >> th></tr> > >> <%= render(:partial => ''line_item'', :collection => @line_items ) %> > >> </table> > >> <br /> > >> TODO: checkout, etc. > >> <% end %> > >> > >> > >> ***app/views/order/_line_item.rhtml > >> <tr> > >> <td><%=h line_item.inspect %></td> > >> </tr> > >> > >> ***app/models/order.rb (just for reference) > >> class Order < ActiveRecord::Base > >> belongs_to :customer > >> has_many :reservations > >> > >> def line_items > >> # indexed by seat group (OK as key because sg uniquely > >> identifies event) > >> # list == {26 => {:name => ''Christmas Glo'', :quantity => > >> 3, :price => > >> 10.0, :ext_price => 30.0}, ...} > >> list = {} > >> reservations.each do |r| > >> if list.has_key? r.seat_group.id > >> list[r.seat_group.id][:quantity] = list[r.seat_group.id] > >> [:quantity] > >> + 1 > >> list[r.seat_group.id][:ext_price] > >> list[r.seat_group.id][:ext_price] + list[r.seat_group.id][:price] > >> else > >> list[r.seat_group.id] = { > >> :name => r.event.name, > >> :quantity => 1, > >> :price => r.seat_group.price, > >> :ext_price => r.seat_group.price, > >> :seat_group => r.seat_group, # TODO: need this? > >> :event => r.seat_group.event # TODO: need this? > >> } > >> end > >> end > >> list > >> end > >> end > >> > >> > >> > >> What''s really weird is that I get this output at the console (from > >> the > >> controller): > >> > >> Line items: > >> [{:quantity=>1, :price=>0.0, :ext_price=>0.0, > >> :seat_group=>#<SeatGroup:0x23edb10 > >> @attributes={"event_id"=>"6", "name"=>"Right Section", "price"=>"0", > >> "venue_id"=>"5", "id"=>"28", "number_of_seats"=>"500"}, > >> @event=#<Event:0x23db94c @attributes={"name"=>"Easter", > >> "event_at"=>"2006-03-23 19:00:00", > >> "ticket_sale_ends_at"=>"2006-03-23 19:00:00", > >> "event_appears_at"=>"2005-09-03 10:51:00", "venue_id"=>"5", > >> "coordinator_user_id"=>"0", "id"=>"6", > >> "ticket_sale_begins_at"=>"2005-09-03 10:51:00", > >> "position"=>"0"}>>, :event=>#<Event:0x23db94c @attributes> >> {"name"=>"Easter", > >> "event_at"=>"2006-03-23 19:00:00", > >> "ticket_sale_ends_at"=>"2006-03-23 19:00:00", > >> "event_appears_at"=>"2005-09-03 10:51:00", "venue_id"=>"5", > >> "coordinator_user_id"=>"0", "id"=>"6", > >> "ticket_sale_begins_at"=>"2005-09-03 10:51:00", > >> "position"=>"0"}>, :name=>"Easter"}] > >> > >> but I get ''nil'' as the sole output from the view. I''m not sure if > >> this whole > >> approach is the best way to do it. Most of the data the view is > >> displaying > >> here is created on-the-fly as an aggregate of the rows in the > >> model. A lot > >> of this info is virtual. I just figured it would be easier to have > >> the model > >> create the data, glue it together in a Hash, and have the view > >> take it > >> apart. Are there any better ways to do this? > >> > >> Thanks so much for all your help! > >> > >> Brad > >> > >> -- > >> Brad Ediger > >> 866-EDIGERS > >> > >> > >> On Sep 5, 2005, at 9:19 PM, Wilson Bilkovich wrote: > >> > >> Try passing @order.line_items.collect { |k,v| v } as the collection. > >> Inside the partial, "<% p line_item -%>" should verify that you are > >> receiving a LineItem object. > >> Also, assuming that''s an ActiveRecord object, "line_item.quantity" > >> seems to be the preferred method these days, instead of > >> line_item[:quantity] > >> It might be helpful to move the collect (or map) call outside of the > >> render. That would give you a place to "p" or "inspect" it before it > >> enters the lair of the beast. > >> > >> That being said, it''s been a long day, and I didn''t actually run any > >> code before writing this message. Heh. > >> > >> --Wilson. > >> > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > >> > >> > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I guess I''ll figure out another way. It''s really weird and I don''t understand. I guess this forces me to refactor my code every once in a while... Thanks for all your help! be -- Brad Ediger 866-EDIGERS On Sep 7, 2005, at 9:16 AM, Wilson Bilkovich wrote:> That''s totally bizarre. Your code looks exactly like some I have in > one of my projects: > <%= render(:partial => ''member'', :collection => @members) %> > > _member.rhtml: > <% @member = member %> > <% p member -%> > etc etc etc > > On 9/6/05, Brad Ediger <brad-sod+mMc99o6+XT7JhA+gdA@public.gmane.org> wrote: > >> Well, no luck here. I changed @line_items to @items and the same >> thing occurs. I added a <% p @items %> just before the render >> (:partial...) and it gives the correct object on the console: >> >> [{:quantity=>2, :price=>0.0, :ext_price=>0.0, :seat_group=>#<SeatGrou >> p:0 >> x242f344 @attributes={"event_id"=>"6", "name"=>"Right Section", >> "price"=>"0", "venue_id"=>"5", "id"=>"28", "number_of_seats"=>"500"}, >> @event=#<Event:0x24215dc @attributes={"name"=>"Easter", >> "event_at"=>"2006-03-23 19:00:00", "ticket_sale_ends_at"=>"2006-03-23 >> 19:00:00", "event_appears_at"=>"2005-09-03 10:51:00", >> "venue_id"=>"5", "coordinator_user_id"=>"0", "id"=>"6", >> "ticket_sale_begins_at"=>"2005-09-03 10:51:00", "position"=>"0"} >> >>>> , :event=>#<Event:0x24215dc @attributes={"name"=>"Easter", >>>> >> "event_at"=>"2006-03-23 19:00:00", "ticket_sale_ends_at"=>"2006-03-23 >> 19:00:00", "event_appears_at"=>"2005-09-03 10:51:00", >> "venue_id"=>"5", "coordinator_user_id"=>"0", "id"=>"6", >> "ticket_sale_begins_at"=>"2005-09-03 10:51:00", "position"=>"0"} >> >>> , :name=>"Easter"}] >>> >> >> I don''t understand!! >> -- >> Brad Ediger >> 866-EDIGERS >> >> >> On Sep 6, 2005, at 4:16 PM, Wilson Bilkovich wrote: >> >> >>> Sorry for the short reply to your content-filled post; >>> What happens if you choose a different name for the collection than >>> "@line_items", such as "@test_somethings"? >>> I don''t have the source in front of me at the moment, but I recall >>> there being some "automagic" thing regarding collections with the >>> pluralized name of a partial. >>> I pray that this doesn''t fix your problem, because that would be too >>> crazy to believe.. but it might be worth a try. >>> >>> Also, just to make sure we''re not missing something simple, try >>> adding: >>> <% p @line_items -%> in the main view, right before the render >>> (:partial) call. >>> >>> --Wilson. >>> >>> On 9/5/05, Brad Ediger <brad-sod+mMc99o6+XT7JhA+gdA@public.gmane.org> wrote: >>> >>> >>>> This seems really weird. To clear up some confusion -- LineItem >>>> isn''t an >>>> ActiveRecord object. It is a Hash of properties that I assembled >>>> ad-hoc to >>>> meet the needs of this view. You''re absolutely right, Wilson -- >>>> the map call >>>> fits better in the controller. But this object doesn''t seem to be >>>> getting >>>> from the controller to the view. There has to be something I''m >>>> missing. Let >>>> the code tell it: (I''ve bolded the lines of code that follow the >>>> object from >>>> the controller to the view.) >>>> >>>> ***app/controllers/order_controller.rb >>>> class OrderController < ApplicationController >>>> def show_cart >>>> if session[:cart].nil? >>>> @order = nil >>>> @line_items = nil >>>> else >>>> @order = Order.find(session[:cart]) >>>> @line_items = @order.line_items.collect { |k,v| v } >>>> puts "Line items: " >>>> puts @line_items.inspect # for console debugging >>>> end >>>> end >>>> end >>>> >>>> ***app/views/order/show_cart.rhtml >>>> <h1>Your Cart</h1> >>>> >>>> <% if @order.nil? || @order.reservations == [] %> >>>> You have no items in your cart. <%= link_to(''Find >>>> Events'', :controller => >>>> ''events'', :action => ''list'') %> >>>> <% else %> >>>> <table> >>>> <tr><th>Event</th><th>Unit Price</th><th>Qty</th><th>Ext. Price</ >>>> th></tr> >>>> <%= render(:partial => ''line_item'', :collection => @line_items ) %> >>>> </table> >>>> <br /> >>>> TODO: checkout, etc. >>>> <% end %> >>>> >>>> >>>> ***app/views/order/_line_item.rhtml >>>> <tr> >>>> <td><%=h line_item.inspect %></td> >>>> </tr> >>>> >>>> ***app/models/order.rb (just for reference) >>>> class Order < ActiveRecord::Base >>>> belongs_to :customer >>>> has_many :reservations >>>> >>>> def line_items >>>> # indexed by seat group (OK as key because sg uniquely >>>> identifies event) >>>> # list == {26 => {:name => ''Christmas Glo'', :quantity => >>>> 3, :price => >>>> 10.0, :ext_price => 30.0}, ...} >>>> list = {} >>>> reservations.each do |r| >>>> if list.has_key? r.seat_group.id >>>> list[r.seat_group.id][:quantity] = list[r.seat_group.id] >>>> [:quantity] >>>> + 1 >>>> list[r.seat_group.id][:ext_price] >>>> list[r.seat_group.id][:ext_price] + list[r.seat_group.id][:price] >>>> else >>>> list[r.seat_group.id] = { >>>> :name => r.event.name, >>>> :quantity => 1, >>>> :price => r.seat_group.price, >>>> :ext_price => r.seat_group.price, >>>> :seat_group => r.seat_group, # TODO: need this? >>>> :event => r.seat_group.event # TODO: need this? >>>> } >>>> end >>>> end >>>> list >>>> end >>>> end >>>> >>>> >>>> >>>> What''s really weird is that I get this output at the console (from >>>> the >>>> controller): >>>> >>>> Line items: >>>> [{:quantity=>1, :price=>0.0, :ext_price=>0.0, >>>> :seat_group=>#<SeatGroup:0x23edb10 >>>> @attributes={"event_id"=>"6", "name"=>"Right Section", >>>> "price"=>"0", >>>> "venue_id"=>"5", "id"=>"28", "number_of_seats"=>"500"}, >>>> @event=#<Event:0x23db94c @attributes={"name"=>"Easter", >>>> "event_at"=>"2006-03-23 19:00:00", >>>> "ticket_sale_ends_at"=>"2006-03-23 19:00:00", >>>> "event_appears_at"=>"2005-09-03 10:51:00", "venue_id"=>"5", >>>> "coordinator_user_id"=>"0", "id"=>"6", >>>> "ticket_sale_begins_at"=>"2005-09-03 10:51:00", >>>> "position"=>"0"}>>, :event=>#<Event:0x23db94c @attributes>>>> {"name"=>"Easter", >>>> "event_at"=>"2006-03-23 19:00:00", >>>> "ticket_sale_ends_at"=>"2006-03-23 19:00:00", >>>> "event_appears_at"=>"2005-09-03 10:51:00", "venue_id"=>"5", >>>> "coordinator_user_id"=>"0", "id"=>"6", >>>> "ticket_sale_begins_at"=>"2005-09-03 10:51:00", >>>> "position"=>"0"}>, :name=>"Easter"}] >>>> >>>> but I get ''nil'' as the sole output from the view. I''m not sure if >>>> this whole >>>> approach is the best way to do it. Most of the data the view is >>>> displaying >>>> here is created on-the-fly as an aggregate of the rows in the >>>> model. A lot >>>> of this info is virtual. I just figured it would be easier to have >>>> the model >>>> create the data, glue it together in a Hash, and have the view >>>> take it >>>> apart. Are there any better ways to do this? >>>> >>>> Thanks so much for all your help! >>>> >>>> Brad >>>> >>>> -- >>>> Brad Ediger >>>> 866-EDIGERS >>>> >>>> >>>> On Sep 5, 2005, at 9:19 PM, Wilson Bilkovich wrote: >>>> >>>> Try passing @order.line_items.collect { |k,v| v } as the >>>> collection. >>>> Inside the partial, "<% p line_item -%>" should verify that you are >>>> receiving a LineItem object. >>>> Also, assuming that''s an ActiveRecord object, "line_item.quantity" >>>> seems to be the preferred method these days, instead of >>>> line_item[:quantity] >>>> It might be helpful to move the collect (or map) call outside of >>>> the >>>> render. That would give you a place to "p" or "inspect" it >>>> before it >>>> enters the lair of the beast. >>>> >>>> That being said, it''s been a long day, and I didn''t actually run >>>> any >>>> code before writing this message. Heh. >>>> >>>> --Wilson. >>>> >>>> >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>> >>>> >>>> >>>> >>>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >