2012-09-26 22 views
0

我目前正在關注敏捷Web開發書(Rails 3),並且一直存在私有方法錯誤。經過數小時的研究,我一直無法找到解決方案。ROR - 購物車中的NoMethodError#show - 私有方法錯誤

以下是我的工作,並已發現的字符串,給出了問題的代碼是:

<td class="item_price"><%= number_to_currency(item.total_price) %></td> 

有已張貼在該解決方案是將行類似的問題/解決方案項目類別在私有上方,但是,訂單項類別不是私有的。

任何幫助,將不勝感激。

CODE

<% if notice %> 
<p id="notice"><%= notice %></p> 
<% end %> 

<!-- START_HIGHLIGHT --> 
<h2>Your Cart</h2> 
<table> 
<!-- END_HIGHLIGHT --> 
    <% @cart.line_items.each do |item| %> 
<!-- START_HIGHLIGHT --> 
    <tr> 
     <td><%= item.quantity %>&times;</td> 
     <td><%= item.product.title %></td> 
     <td class="item_price"><%= number_to_currency(item.total_price) %></td> 
    </tr> 
<!-- END_HIGHLIGHT --> 
    <% end %> 
<!-- START_HIGHLIGHT --> 
    <tr class="total_line"> 
    <td colspan="2">Total</td> 
    <td class="total_cell"><%= number_to_currency(@cart.total_price) %></td> 
    </tr> 
<!-- END_HIGHLIGHT --> 
<!-- START_HIGHLIGHT --> 
</table> 
<!-- END_HIGHLIGHT --> 

<%= button_to 'Empty cart', @cart, method: :delete, 
    data: { confirm: 'Are you sure?' } %> 

錯誤

private method `total_price' called for #<LineItem 

LINE_ITEM模型

class LineItem < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :cart 
    attr_accessible :cart_id, :product_id 
end 

def total_price 
    product.price * quantity 
end 

車模型

class Cart < ActiveRecord::Base 
    has_many :line_items, dependent: :destroy 

    def add_product(product_id) 
    current_item = line_items.find_by_product_id(product_id) 
    if current_item 
     current_item.quantity += 1 
    else 
     current_item = line_items.build(product_id: product_id) 
    end 
    current_item 
    end 

    def total_price 
    line_items.to_a.sum { |item| item.total_price } 
    end 
end 

線項目控制器

class LineItemsController < ApplicationController 
    # GET /line_items 
    # GET /line_items.json 
    def index 
    @line_items = LineItem.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @line_items } 
    end 
    end 

    # GET /line_items/1 
    # GET /line_items/1.json 
    def show 
    @line_item = LineItem.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @line_item } 
    end 
    end 

    # GET /line_items/new 
    # GET /line_items/new.json 
    def new 
    @line_item = LineItem.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @line_item } 
    end 
    end 

    # GET /line_items/1/edit 
    def edit 
    @line_item = LineItem.find(params[:id]) 
    end 

    # POST /line_items 
    # POST /line_items.json 
    def create 
    @cart = current_cart 
    product = Product.find(params[:product_id]) 
    @line_item = @cart.add_product(product.id) 
    @line_item.product = product 

    respond_to do |format| 
     if @line_item.save 
     format.html { redirect_to @line_item.cart, 
      notice: 'Line item was successfully created.' } 
     format.json { render json: @line_item, 
      status: :created, location: @line_item } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @line_item.errors, 
     status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /line_items/1 
    # PUT /line_items/1.json 
    def update 
    @line_item = LineItem.find(params[:id]) 

    respond_to do |format| 
     if @line_item.update_attributes(params[:line_item]) 
     format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @line_item.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /line_items/1 
    # DELETE /line_items/1.json 
    def destroy 
    @line_item = LineItem.find(params[:id]) 
    @line_item.destroy 

    respond_to do |format| 
     format.html { redirect_to line_items_url } 
     format.json { head :no_content } 
    end 
    end 
end 
+0

你可以發佈你的控制器的代碼? –

+0

請提供您的模型端代碼(具有'total_price'方法的文件)? – Salil

+0

感謝Robert和Salil。我已將控制器和型號代碼添加到原始帖子中。 – PDX

回答

0

在您的LineItem模型中total_price定義在LineItem類的外部。所以它應該是:

class LineItem < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :cart 
    attr_accessible :cart_id, :product_id 

    def total_price 
    product.price * quantity 
    end 
end 
+0

Thanks @Casual Coder,就是解決方案。感謝大家的幫助,非常感謝。 – PDX

+0

很高興我能幫到你。乾杯。 –

0

您是說@cart.total_price是給錯誤,但你的錯誤顯示,它是在LineItem模型尋找這個功能。這可能意味着您還必須在LineItem模型中使用total_price方法。你有嗎?是的,正如其他人所說,最好在你的文章中添加你的控制器和型號代碼。

編輯:

基於在後您的更新,是該total_price方法應該是在裏面你的類。這是否解決了這個問題?

+0

感謝@Samiron的快速反應。對不起,我粘貼了錯誤的字符串,應該是: 'code'<%= number_to_currency(item.total_price)%> – PDX