我在calculation_of_total_cost
model Tippy
的方法
它遇到了index.html.erb
通过tippies
个视图目录调用的问题。
这是我收到的错误:undefined method
*'为零:NilClass`
我用谷歌搜索过,现在明白这是其中一个变量为nil
的结果。
如何解决此问题,即如何使方法在index.html.erb
中运行?这是我从中调用它的索引视图,所以我需要一个实例方法,而不是类,对吧?
另外,附录:这个方法在show.html.erb
show.html.erb
<br/><br/>
<h1 class="text-center">Your Total Cost</h1>
<br/><br />
<table class="table table-striped">
<tr>
<td>
Cost of Your Meal:
</td>
<td>
<%= humanized_money_with_symbol @tippy.cost %>
</td>
</tr>
<tr>
<td>
Tip You Picked:
</td>
<td>
<%= number_to_percentage(@tippy.tip * 100, format: "%n%", precision: 0) %>
</td>
</tr>
<tr>
<td>
The Total Cost:
</td>
<td>
<%= humanized_money_with_symbol @tippy.calculation_of_total_cost %>
</td>
</tr>
</table>
<%= link_to 'New Tippy', new_tippy_path %>
<%= link_to "Index", tippies_path %>
以下是Tippy
型号:
class Tippy < ApplicationRecord
validates :tip, presence: true
validates :cost, presence: true
#monetize :tip_cents
monetize :cost_cents, :numericality => {:greater_than => 0}
TIP_CHOICES = { "10%" => ".10", "20%" => ".20", "30%" => ".30", "40%" => ".40", "50%" => ".50",
"60%" => ".60", "70%" => ".70", "80%" => ".80", "90%" => ".90" }
def calculation_of_total_cost
cost + (tip * cost)
end
end
以下是index.html.erb
文件
<p id="notice"><%= notice %></p>
<h1>Tippies</h1>
<table>
<thead>
<tr>
<th>Tip</th>
<th>Cost</th>
<th>Total</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @tippies.each do |tippy| %>
<tr>
<td><%= tippy.tip %></td>
<td><%= tippy.cost %></td>
<td><%= tippy.calculation_of_total_cost %></td>
<td><%= link_to 'Show', tippy %></td>
<td><%= link_to 'Edit', edit_tippy_path(tippy) %></td>
<td><%= link_to 'Destroy', tippy, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Tippy', new_tippy_path %>
Tippy Controller
class TippiesController < ApplicationController
#before_action :set_tippy, only: [:show, :edit, :update, :destroy]
# GET /tippies
# GET /tippies.json
def index
@tippies = Tippy.all
end
# GET /tippies/1
# GET /tippies/1.json
def show
#@calculation_of_total_cost
end
# GET /tippies/new
def new
@tippy = Tippy.new
end
# GET /tippies/1/edit
def edit
end
# POST /tippies
# POST /tippies.json
def create
@tippy = Tippy.new(tippy_params)
respond_to do |format|
if @tippy.save
format.html { redirect_to @tippy, notice: 'Tippy was successfully created.' }
format.json { render :show, status: :created, location: @tippy }
else
format.html { render :new }
format.json { render json: @tippy.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tippies/1
# PATCH/PUT /tippies/1.json
def update
respond_to do |format|
if @tippy.update(tippy_params)
format.html { redirect_to @tippy, notice: 'Tippy was successfully updated.' }
format.json { render :show, status: :ok, location: @tippy }
else
format.html { render :edit }
format.json { render json: @tippy.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tippies/1
# DELETE /tippies/1.json
def destroy
@tippy.destroy
respond_to do |format|
format.html { redirect_to tippies_url, notice: 'Tippy was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tippy
@tippy = Tippy.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def tippy_params
params.require(:tippy).permit(:tip, :cost)
end
end
答案 0 :(得分:0)
要解决这个问题,你需要在index.html.erb的这一行设置 binding.pry 或断点,以便我们可以在循环中理解你正在执行为什么tippy正在获取价值为零。
您需要安装pry
gem。
还请分享@tippies的值和循环中失败的另一个变量的细节,因为tippy = nil。
pry的替代方法是使用puts tippy.calculation_of_total_cost
在日志中打印变量的值。
现在我猜我的@tippies
表中包含所有@tippy
的{{1}}可能有一个tippies
字段。要验证这一点,您应该在calculation of total cost = nil
视图中使用调试检查tippy和tippy.calculation_of_total_cost
的值。
index.html.erb
最好还是检查<% @tippies.each do |tippy| %>
<tr>
<% binding.pry %>
<td><%= tippy.tip %></td>
<td><%= tippy.cost %></td>
<td><%= tippy.calculation_of_total_cost %></td>
<td><%= link_to 'Show', tippy %></td>
<td><%= link_to 'Edit', edit_tippy_path(tippy) %></td>
<td><%= link_to 'Destroy', tippy, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
是否有效。
show.html.erb