我目前正在关注《敏捷 Web 开发》一书(Rails 3),并且遇到了私有方法错误。经过数小时的研究,我无法找到解决方案。
以下是我正在使用的代码,并发现给出问题的字符串是:
<td class="item_price"><%= number_to_currency(item.total_price) %></td>
已经发布了一个类似的问题/解决方案,解决方案是将行项目类放在私有之上,但是,行项目类不是私有的。
任何帮助,将不胜感激。
代码
<% 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 %>×</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