Hi
I am new to Rails, and I was trying to follow the "Agile Web Development
With Rails" textbook. As I was trying to create the "Add_to_cart"
application, I found that when I clicked the "Add to Cart" link on my
http://localhost:3000/store page, I got the following error:
NoMethodError in Store#display_cart
===========================Showing app/views/store/display_cart.rhtml where line
#20 raised: You have
a nil object when you didn''t expect it!
The error occurred while evaluating nil.product
Extracted source (around line #20): 17: </tr>
18: <%
19: for item in @items
20: product = item.product
21: -%>
22: <tr>
23: <td><%= item.quantity %></td>
Just for pointer, I had actually got to the correct page once, but then I
skipped the "Iteration C2: Handling Errors" part in the tutorial and
went
straight to the "8.5 Iteration C3: Finishing the Cart" part, and
implemented
the empty_cart function there. I have been getting this problem since then
(when I implemented this function and clicked the link in browser and then
came back to add more objects to the cart). Could it be that I have deleted
the object or something? After some googling I thought that the problem was
actually related to the session, and so i tried to clear the session by
using:
1. rake db:sessions:clear
2. rake tmp:sessions:clear
But even this did not solve the problem. So I am clueless as of now. Below
is the code for reference:
in store_controller.rb
============== def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ''display_cart'')
end
def display_cart
@cart = find_cart
@items = @cart.items
if @items.empty?
redirect_to_index("Your cart is currently empty")
end
end
def find_cart
session[:cart] ||= Cart.new
end
===================
Any help would be really appreciated !!!
Thanks,
Kapil
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Hi I am still waiting for a reply !! Thanks, Kapil On 7/15/07, kapil kaushik <kaushik.kapil-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi > > I am new to Rails, and I was trying to follow the "Agile Web Development > With Rails" textbook. As I was trying to create the "Add_to_cart" > application, I found that when I clicked the "Add to Cart" link on my > http://localhost:3000/store page, I got the following error: > > NoMethodError in Store#display_cart > ===========================> Showing app/views/store/display_cart.r html where line #20 raised: You > have a nil object when you didn''t expect it! > The error occurred while evaluating nil.product > > Extracted source (around line #20): 17: </tr> > 18: <% > 19: for item in @items > 20: product = item.product > 21: -%> > 22: <tr> > 23: <td><%= item.quantity %></td> > > Just for pointer, I had actually got to the correct page once, but then I > skipped the "Iteration C2: Handling Errors" part in the tutorial and went > straight to the " 8.5 Iteration C3: Finishing the Cart" part, and > implemented the empty_cart function there. I have been getting this problem > since then (when I implemented this function and clicked the link in browser > and then came back to add more objects to the cart). Could it be that I have > deleted the object or something? After some googling I thought that the > problem was actually related to the session, and so i tried to clear the > session by using: > > 1. rake db:sessions:clear > 2. rake tmp:sessions:clear > > But even this did not solve the problem. So I am clueless as of now. Below > is the code for reference: > > in store_controller.rb > ==============> def add_to_cart > product = Product.find(params[:id]) > @cart = find_cart > @cart.add_product(product) > redirect_to(:action => ''display_cart'') > end > > def display_cart > @cart = find_cart > @items = @cart.items > if @items.empty? > redirect_to_index("Your cart is currently empty") > end > end > > def find_cart > session[:cart] ||= Cart.new > end > ===================> > Any help would be really appreciated !!! > > Thanks, > Kapil >-- I can sum up everything i learned about life in three words -- IT GOES ON !! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I have been waiting for some help from the gurus out here for last 5 days. Anything I have been trying is not working !! Thanks, Kapil On 7/15/07, kapil kaushik <kaushik.kapil-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi > > I am new to Rails, and I was trying to follow the "Agile Web Development > With Rails" textbook. As I was trying to create the "Add_to_cart" > application, I found that when I clicked the "Add to Cart" link on my > http://localhost:3000/store page, I got the following error: > > NoMethodError in Store#display_cart > ===========================> Showing app/views/store/display_cart.r html where line #20 raised: You > have a nil object when you didn''t expect it! > The error occurred while evaluating nil.product > > Extracted source (around line #20): 17: </tr> > 18: <% > 19: for item in @items > 20: product = item.product > 21: -%> > 22: <tr> > 23: <td><%= item.quantity %></td> > > Just for pointer, I had actually got to the correct page once, but then I > skipped the "Iteration C2: Handling Errors" part in the tutorial and went > straight to the " 8.5 Iteration C3: Finishing the Cart" part, and > implemented the empty_cart function there. I have been getting this problem > since then (when I implemented this function and clicked the link in browser > and then came back to add more objects to the cart). Could it be that I have > deleted the object or something? After some googling I thought that the > problem was actually related to the session, and so i tried to clear the > session by using: > > 1. rake db:sessions:clear > 2. rake tmp:sessions:clear > > But even this did not solve the problem. So I am clueless as of now. Below > is the code for reference: > > in store_controller.rb > ==============> def add_to_cart > product = Product.find(params[:id]) > @cart = find_cart > @cart.add_product(product) > redirect_to(:action => ''display_cart'') > end > > def display_cart > @cart = find_cart > @items = @cart.items > if @items.empty? > redirect_to_index("Your cart is currently empty") > end > end > > def find_cart > session[:cart] ||= Cart.new > end > ===================> > Any help would be really appreciated !!! > > Thanks, > Kapil >-- I can sum up everything i learned about life in three words -- IT GOES ON !! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Everything looks normal at first sight... what does your @items collection look like ? Do "p @items" in your controller, it will be displayed in your console. -- 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 -~----------~----~----~----~------~----~------~--~---
Hi Stijn , Thanks !! On printing the items, I saw that my product did not have any order_id. On thorough analysis of my code, I found that I had mistakenly added an extra constructor in the line_items.rb file which was causing the problem. But it took me decent amount of time to do the code review. Things are working fine now. Apologies for bothering the community unnecessarily. Regards, Kapil But i had thought that the On 7/20/07, Stijn Pint <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Everything looks normal at first sight... > what does your @items collection look like ? Do "p @items" in your > controller, it will be displayed in your console. > > -- > Posted via http://www.ruby-forum.com/. > > > >-- I can sum up everything i learned about life in three words -- IT GOES ON !! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---