Emil Gustafsson
2006-Mar-05 20:33 UTC
[Rails] "Undefined method" problem in "Agile Web Development With Rails"
Hi, I''m reading the "Agile Web Development With Rails" book. So long, everything has worked fine, but now I got this annoying error message that I just can''t understand. The error appears for me around page 87, chapter 8. NoMethodError in Store#display_cart Showing app/views/store/display_cart.rhtml where line #28 raised: undefined method `product'' for 2395.0:Float Extracted source (around line #28): 25: </tr> 26: <% 27: for item in @items 28: product = item.product 29: -%> 30: <tr> 31: <td><%= h(product.title) %></td> I''m sure my source code is correct. //Emil -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060305/7062e320/attachment-0001.html
David Mitchell
2006-Mar-05 20:43 UTC
[Rails] "Undefined method" problem in "Agile Web Development With Rails"
For some reason your object "item" has the value 2395.0, and is a floating point object. Without having the Agile book at my fingertips, from memory it''s supposed to have a price attribute with that value. At a guess, you could be setting @items to an individual item, rather than a collection of items. That would be consistent with the error you''re seeing. Have a look at your controller code where you set @items to something; the problem''s probably going to be right around there. HTH Dave M. On 06/03/06, Emil Gustafsson <emil@classicbildelar.com> wrote:> Hi, > > I''m reading the "Agile Web Development With Rails" book. So long, everything > has worked fine, but now I got this annoying error message that I just can''t > understand. The error appears for me around page 87, chapter 8. > > > > NoMethodError in Store#display_cart > > Showing app/views/store/display_cart.rhtml where line #28 > raised: > undefined method `product'' for 2395.0:Float > > > > > Extracted source (around line #28): > 25: </tr> > 26: <% > 27: for item in @items > 28: product = item.product > 29: -%> > 30: <tr> > 31: <td><%= h(product.title) %></td> > > > I''m sure my source code is correct. > > //Emil > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Emil Gustafsson
2006-Mar-05 21:48 UTC
[Rails] "Undefined method" problem in "Agile Web Development With Rails"
This is the code in the controller where @items is set:
def display_cart
@cart = find_cart
@items = @cart.items
end
private
def find_cart
session[:cart] ||= Cart.new
end
And this is the code in the cart.rb model:
class Cart
attr_reader :items
attr_reader :total_price
def initialize
@items = []
@total_price = 0.0
end
def add_product(product)
@items << LineItem.for_product(product)
@total_price += product.price
end
end
Any idea?
5 mar 2006 kl. 21.43 - 5 mar skrev David Mitchell:
> For some reason your object "item" has the value 2395.0, and is a
> floating point object. Without having the Agile book at my
> fingertips, from memory it''s supposed to have a price attribute
with
> that value.
>
> At a guess, you could be setting @items to an individual item, rather
> than a collection of items. That would be consistent with the error
> you''re seeing.
>
> Have a look at your controller code where you set @items to something;
> the problem''s probably going to be right around there.
>
> HTH
>
> Dave M.
>
> On 06/03/06, Emil Gustafsson <emil@classicbildelar.com> wrote:
>> Hi,
>>
>> I''m reading the "Agile Web Development With Rails"
book. So long,
>> everything
>> has worked fine, but now I got this annoying error message that I
>> just can''t
>> understand. The error appears for me around page 87, chapter 8.
>>
>>
>>
>> NoMethodError in Store#display_cart
>>
>> Showing app/views/store/display_cart.rhtml where line #28
>> raised:
>> undefined method `product'' for 2395.0:Float
>>
>>
>>
>>
>> Extracted source (around line #28):
>> 25: </tr>
>> 26: <%
>> 27: for item in @items
>> 28: product = item.product
>> 29: -%>
>> 30: <tr>
>> 31: <td><%= h(product.title) %></td>
>>
>>
>> I''m sure my source code is correct.
>>
>> //Emil
>> _______________________________________________
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
H?lsningar,
Emil
emil@gstfssn.com
Josh Kieschnick
2006-Mar-22 04:39 UTC
[Rails] Re: "Undefined method" problem in "Agile Web Development Wit
I am working through that book as well. I am getting the exact same error. I thought it was something I did wrong but I have no idea what to check since I''m still pretty new at this stuff. Hopefully, it''s the syntax of the book. Has anyone seen the errata or have any ideas? I would think that if we''re getting the same results, surely there''s someone else out there who knows what''s going on. -- Posted via http://www.ruby-forum.com/.