RecordNotFound:无法找到“id”级别

修复此错误时遇到一些问题:

ActiveRecord::RecordNotFound (Couldn't find Level with 'id'=1 [WHERE "levels"."habit_id" = ?]): app/controllers/days_missed_controller.rb:9:in `create' 

点击此按钮时:

 <%= link_to ''.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  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 

这是它的要点

这个问题来自@ Pavan的回答 。 我们无法弄清楚如何一起解决这个错误,所以我们非常感谢您的意见!

在habit.rb中添加方法current_habit_level ,它将使用current_level并获取该习惯的level_id。 因此,当您单击链接时,它将传递正确的级别,并且在搜索时不会崩溃

  def current_habit_level self.levels.order("id asc").limit(current_level).last end 

使用此方法获取链接中的级别ID