Rails:没有路线匹配...缺少必需的键:[:id] [英] Rails: No route matches ... missing required keys: [:id]

查看:64
本文介绍了Rails:没有路线匹配...缺少必需的键:[:id]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完全错误:

No route matches {:id=>#<Reminder id: nil, medication: nil, time: nil, created_at: nil, updated_at: nil, user_id: 1>} missing required keys: [:id]

这是导致错误的index.html.erb中的代码:

Here is the code from index.html.erb that is causing the error:

<tbody>
    <% @reminders.each do |reminder| %>
      <tr <%= dom_id(reminder) %>>
        <td><%= reminder.medication %></td>
        <td><%= reminder.time %></td>
        <td>
          <%= link_to "Edit", edit_reminder_path(reminder) %>
          or
          <%= link_to 'Remove', reminder, method: :delete, data: { confirm: 'Are you sure?' } %>
      </td>
      </tr>
    <% end %>
  </tbody>

型号:

class Reminder < ActiveRecord::Base
    validates :medication, presence: true
    belongs_to :user
end
class User < ActiveRecord::Base
  has_many :reminders
end

操作:

  def index
    @reminders = current_user.reminders
    @reminder = current_user.reminders.new 
  end
  def edit
  end

路线:

Medy::Application.routes.draw do
  devise_for :users
  resources :reminders
  root 'reminders#index'
end

是否需要在编辑操作中添加一些内容才能使这项工作正常?

Do I need to add something to the edit action to make this work?

我将 @ reminders = Reminders.all 更改为 @ reminders = current_user之后,错误开始发生.reminders 在索引操作中。

The error started happening after I changed @reminders = Reminders.all to @reminders = current_user.reminders in the index action.

推荐答案

原因是您传递的是未保存的实例提醒到edit_reminder_path。当您将实例作为参数传递给路由时,将在该实例上调用 to_param方法。默认情况下, to_param方法返回实例的 id。

The reason for this is that you are passing an unsaved instanced of 'reminder' into the edit_reminder_path. When you pass an instance as a parameter to a route the 'to_param' method is called on that instance. By default the 'to_param' method returns the 'id' of the instance.

在您的情况下,您具有:

In your case you have:

def index
  @reminders = current_user.reminders
  @reminder = current_user.reminders.new
end

因为您已将@reminder的作用域设置为当前用户。该实例将添加到该用户的提醒集合中,因此也包含在@reminders中。这意味着当您在索引页面上呈现@reminders时,它还将尝试呈现尚未设置'id'的未保存的@reminder。

Because you have scoped the @reminder to the current user. That instance is added to the collection of reminders for that user, and so is also included in @reminders. This means that when you are rendering out the @reminders on the index page, It also tries to render out the unsaved @reminder which has not yet got an 'id' set.

一个更好的解决方案是将索引操作更改为:

A better solution would be to change you index action to:

def index
  @reminders = current_user.reminders
  @reminder = Reminder.new
end

您可以保存提醒,很可能是在创建操作中,将范围设置为current_user:

And then at the point that you save the reminder, most likely in a 'create' action, you would scope to the current_user:

def create
  @reminder = current_user.reminders.new(reminder_params)
  if @reminder.save .....
end

这篇关于Rails:没有路线匹配...缺少必需的键:[:id]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆