我目前正在关注敏捷Web开发手册(Rails 3)并且遇到了私有方法错误。经过数小时的研究,我找不到解决方案。
以下是我正在使用的代码,并发现了提供问题的字符串:
<td class="item_price"><%= number_to_currency(item.total_price) %></td>
有一个类似的问题/解决方案已经发布,其中解决方案是将Item类放在私有上面,但是,行项类不是私有的。
任何帮助都将不胜感激。
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 %>×</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)
您说@cart.total_price
正在提供错误,但您的错误表明它正在LineItem
模型中查找此功能。这可能意味着您必须在total_price
模型中使用LineItem
方法。你有吗?是的,正如其他人所说,最好在你的帖子中添加控制器和型号代码。
修改强> 的
根据您在帖子中的更新,是total_price
方法应该在您的课程内。这会解决问题吗?
答案 1 :(得分: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