Ruby on rails 3 ROR—推车中的NoMethodError#show—私有方法错误
我目前正在学习《敏捷Web开发》一书(Rails 3),并且一直被一个私有方法错误所困扰。经过数小时的研究,我一直未能找到解决办法 以下是我正在使用的代码,我发现了导致问题的字符串:Ruby on rails 3 ROR—推车中的NoMethodError#show—私有方法错误,ruby-on-rails-3,private,nomethoderror,Ruby On Rails 3,Private,Nomethoderror,我目前正在学习《敏捷Web开发》一书(Rails 3),并且一直被一个私有方法错误所困扰。经过数小时的研究,我一直未能找到解决办法 以下是我正在使用的代码,我发现了导致问题的字符串: <td class="item_price"><%= number_to_currency(item.total_price) %></td> 有一个类似的问题/解决方案已经发布,解决方案是将行项目类放在private之上,但是,行项目类不是private 任何帮助都将不胜
<td class="item_price"><%= number_to_currency(item.total_price) %></td>
有一个类似的问题/解决方案已经发布,解决方案是将行项目类放在private之上,但是,行项目类不是private
任何帮助都将不胜感激
代码
你的车
&时代;
全部的
错误
private方法'total_price'调用了#您说的是@cart.total_price
给出了错误,但您的错误表明它正在LineItem
模型中查看此函数。这可能意味着您在LineItem
模型中也必须有total\u price
方法。有吗?是的,正如其他人所说,最好在帖子中添加控制器和型号代码
编辑:
根据您在帖子中的更新,是的,total_price
方法应该在您的类中。这解决了问题吗?在您的行项目
模型中总价
是在行项目
类之外定义的。因此,它应该是:
class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
attr_accessible :cart_id, :product_id
def total_price
product.price * quantity
end
end
class LineItem
您可以发布控制器的代码吗?请提供您的模型侧代码(包含总价
方法的文件)?谢谢Robert和Salil。我已经在我的原始帖子中加入了控制器和模型代码。谢谢@Samiron的快速回复。抱歉,我粘贴了错误的字符串,应该是:“code”谢谢@Casual Coder,这就是解决方案。谢谢大家的帮助,真的很感激。
private method `total_price' called for #<LineItem
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
class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
attr_accessible :cart_id, :product_id
def total_price
product.price * quantity
end
end