Rails if statement error: "nil can't be coerced into Fixnum"
Rails if statement error: "nil can't be coerced into Fixnum"
该应用程序允许用户对给定的午餐进行投票并对它的整体表现进行评分。每个午餐都与 供应商(餐厅)相关联,当用户访问 供应商的 页面时,用户会看到与该供应商相关的所有午餐的历史记录特定供应商。反过来,每个提供者都与给定的美食相关联。当用户投票支持和反对午餐时,与 @lunch
关联的 :lunchscore
值会更新。在 providers/show.html.erb
页面中,我正在尝试:
- 验证用户有与供应商相关联的午餐
- 将给定提供者的所有午餐分数相加
- 将午餐总分除以午餐次数得到平均分
show.html.erb
<% @provider.lunches.count %>
<% if @provider.lunches.count > 0 %>
<% counter = 0 %>
<% @lunches.each do |lunch| %>
<% counter += lunch.lunchscore %>
<% end %>
<!-- counter = counter + lunch.lunchscore -->
<% provider_score = counter / @provider.lunches.count.round %>
<% end %>
provider.rb
class Provider < ActiveRecord::Base
belongs_to :cuisine
has_many :lunches
validates :name, presence: true, uniqueness: true
validates :cuisine, presence: true
end
lunch.rb
class Lunch < ActiveRecord::Base
belongs_to :provider
default_scope -> { order('created_at DESC') }
validates :date, presence: true, uniqueness: true
validates :provider_id, presence: true
end
为了完成,这是我的(截断的)控制器:
providers_controller.rb
def show
@provider = Provider.find(params[:id])
@lunches = @provider.lunches
@cuisine = @provider.cuisine
end
def create
@provider = Provider.new(provider_params)
if @provider.save
flash.now[:success] = "New Provider Added!"
redirect_to providers_path
else
render 'new'
end
end
lunches_controller.rb
def create
@lunch = Lunch.new(lunch_params)
if @lunch.save
flash.now[:success] = "New Lunch Added!"
redirect_to lunches_path
else
render 'new'
end
end
private
def lunch_params
params.require(:lunch).permit(:date, :liked, :disliked, :enough, :not_enough, :provider_id, :lunchscore)
end
当我尝试执行上述操作时,出现此错误:"nil can't be coerced into Fixnum"
此错误与 <% counter += lunch.lunchscore %>
行有关。
考虑到 show.html.erb
页面中的代码在没有 initial lunchscore 集时中断,我尝试通过以下方式设置 1
的值form_for
hidden_field
,或者直接在 lunchscore 列中写入 1
到数据库。这没有解决问题。
我仍然怀疑这个问题与数据类型或视图中的逻辑有关,但我不确定。
任何人都可以解释这个问题是什么,为什么它会导致我的应用程序崩溃,以及我可以插入什么条件逻辑来防止它发生?
你可以这样避免异常:
<% counter += lunch.lunchscore if lunch.lunchscore %>
但是,整个循环不应该在视图代码中,而应该在模型中——可能是 Provider。
该应用程序允许用户对给定的午餐进行投票并对它的整体表现进行评分。每个午餐都与 供应商(餐厅)相关联,当用户访问 供应商的 页面时,用户会看到与该供应商相关的所有午餐的历史记录特定供应商。反过来,每个提供者都与给定的美食相关联。当用户投票支持和反对午餐时,与 @lunch
关联的 :lunchscore
值会更新。在 providers/show.html.erb
页面中,我正在尝试:
- 验证用户有与供应商相关联的午餐
- 将给定提供者的所有午餐分数相加
- 将午餐总分除以午餐次数得到平均分
show.html.erb
<% @provider.lunches.count %>
<% if @provider.lunches.count > 0 %>
<% counter = 0 %>
<% @lunches.each do |lunch| %>
<% counter += lunch.lunchscore %>
<% end %>
<!-- counter = counter + lunch.lunchscore -->
<% provider_score = counter / @provider.lunches.count.round %>
<% end %>
provider.rb
class Provider < ActiveRecord::Base
belongs_to :cuisine
has_many :lunches
validates :name, presence: true, uniqueness: true
validates :cuisine, presence: true
end
lunch.rb
class Lunch < ActiveRecord::Base
belongs_to :provider
default_scope -> { order('created_at DESC') }
validates :date, presence: true, uniqueness: true
validates :provider_id, presence: true
end
为了完成,这是我的(截断的)控制器:
providers_controller.rb
def show
@provider = Provider.find(params[:id])
@lunches = @provider.lunches
@cuisine = @provider.cuisine
end
def create
@provider = Provider.new(provider_params)
if @provider.save
flash.now[:success] = "New Provider Added!"
redirect_to providers_path
else
render 'new'
end
end
lunches_controller.rb
def create
@lunch = Lunch.new(lunch_params)
if @lunch.save
flash.now[:success] = "New Lunch Added!"
redirect_to lunches_path
else
render 'new'
end
end
private
def lunch_params
params.require(:lunch).permit(:date, :liked, :disliked, :enough, :not_enough, :provider_id, :lunchscore)
end
当我尝试执行上述操作时,出现此错误:"nil can't be coerced into Fixnum"
此错误与 <% counter += lunch.lunchscore %>
行有关。
考虑到 show.html.erb
页面中的代码在没有 initial lunchscore 集时中断,我尝试通过以下方式设置 1
的值form_for
hidden_field
,或者直接在 lunchscore 列中写入 1
到数据库。这没有解决问题。
我仍然怀疑这个问题与数据类型或视图中的逻辑有关,但我不确定。
任何人都可以解释这个问题是什么,为什么它会导致我的应用程序崩溃,以及我可以插入什么条件逻辑来防止它发生?
你可以这样避免异常:
<% counter += lunch.lunchscore if lunch.lunchscore %>
但是,整个循环不应该在视图代码中,而应该在模型中——可能是 Provider。