[Rails] "Undefined method" problem in "Agile Web Development With
Rails"
Emil Gustafsson
emil at classicbildelar.com
Sun Mar 5 21:48:19 GMT 2006
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 at 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 at lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
Hälsningar,
Emil
emil at gstfssn.com
More information about the Rails
mailing list