RecordNotFound:无法找到" id"

时间:2015-07-28 20:00:54

标签: ruby-on-rails ruby

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

 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 

enter image description here

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

这是 Gist

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

1 个答案:

答案 0 :(得分:1)

在habit.rb中添加方法 def current_habit_level self.levels.order("id asc").limit(current_level).last end ,该方法将使用current_level并获取该习惯的level_id。因此,当您点击链接时,它将传递正确的级别,并且在搜索时不会崩溃

In [1]: A = np.arange(16).reshape(4, 4)

In [2]: A
Out[2]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

In [3]: strides = A.itemsize * np.array([8, 2, 4, 1])

In [4]: x = p.lib.stride_tricks.as_strided(A, shape = (2, 2, 2, 2), strides = strides)

In [4]: x
Out[4]:
array([[[[ 0,  1],
         [ 4,  5]],

        [[ 2,  3],
         [ 6,  7]]],


       [[[ 4,  5],
         [ 8,  9]],

        [[ 6,  7],
         [10, 11]]]])

In [5]: x.reshape(4, 2, 2)
Out[5]:
array([[[ 0,  1],
        [ 4,  5]],

       [[ 2,  3],
        [ 6,  7]],

       [[ 8,  9],
        [12, 13]],

       [[10, 11],
        [14, 15]]])

使用此方法获取链接

中的级别ID