RecordNotFound:找不到带有“id"的关卡 [英] RecordNotFound: Couldn't find Level with "id"
本文介绍了RecordNotFound:找不到带有“id"的关卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在修复此错误时遇到一些问题:
Having some trouble fixing this error:
ActiveRecord::RecordNotFound (Couldn't find Level with 'id'=1 [WHERE "levels"."habit_id" = ?]):
app/controllers/days_missed_controller.rb:9:in `create'
点击此按钮时:
<%= link_to '<span class="glyphicon glyphicon-remove"></span>'.html_safe, habit_level_days_missed_index_path({ habit_id: habit, level_id: habit.current_level }), remote: true, method: 'post', class: 'habit-check' %> # habits/_habit.html.erb, which is rendered by <%= render @habits %> in pages/home.html.erb
days_missed_controller
def create
habit = Habit.find(params[:habit_id])
habit.missed_days = habit.missed_days + 1
@habit.save!
level = habit.levels.find(params[:level_id])
level.missed_days = level.missed_days + 1
if level.missed_days == 3
level.missed_days = 0
level.days_lost += habit.calculate_days_lost + 2
end
level.save!
head :ok # this returns an empty response with a 200 success status code
end
这是要点.
Here's the Gist of it.
这个问题来自@Pavan 的 回答.我们不知道如何一起解决这个错误,因此我们非常感谢您的意见!
This question is coming off of @Pavan's answer. We couldn't figure out how to solve this error together so we'd greatly appreciate your input!
推荐答案
在习惯.rb 中添加方法 current_habit_level
,该方法将使用 current_level 并为该习惯获取 level_id.这样当您点击链接时,它会通过正确的级别,并且不会在搜索时崩溃
Add method current_habit_level
in habit.rb which will use current_level and fetch level_id for that habit. So that when you click the link it will pass correct level is and it won't crash on searching
def current_habit_level
self.levels.order("id asc").limit(current_level).last
end
使用此方法获取链接中的级别 id
Use this method to fetch level id in link
这篇关于RecordNotFound:找不到带有“id"的关卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文