如何为#<Book::ActiveRecord_Relation:0x007fb709a6a8c0>解决未定义的方法`to_key'? [英] How to solve undefined method `to_key' for #<Book::ActiveRecord_Relation:0x007fb709a6a8c0>?

查看:40
本文介绍了如何为#<Book::ActiveRecord_Relation:0x007fb709a6a8c0>解决未定义的方法`to_key'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了未定义方法 `to_key' 的问题

这是我的 books_controller.rb

 class BooksController <应用控制器定义索引@books = Book.where(user_id: current_user.id)结尾结尾

和我的索引页如下.

index.html.erb

<%= form_for @books 做 |f|%>......<%结束%>

现在,当我要访问索引页面时,出现如下错误.

未定义的方法`to_key' #

解决方案

index 通常返回一个集合.事实上,您的控制器符合要求.但是,您的视图会尝试为其定义表单.正如你所发现的,这不会成功.表单用于实体,而不用于集合.该错误在您的视野中以及您希望如何处理 index.

I am facing problems with undefined method `to_key'

this is my books_controller.rb

 class BooksController  < ApplicationController
   def index
    @books = Book.where(user_id: current_user.id)
    end
 end

and my index page as follow.

index.html.erb

<div>
  <%= form_for @books do |f| %>
...
...
    <% end %>
</div>

now when I am going to access index page that time I got an error as follow.

undefined method `to_key' for #<Book::ActiveRecord_Relation:0x007fb709a6a8c0>

解决方案

index usually returns a collection. And indeed, your controller conforms. However, your view tries to define a form for it. This is not going to succeed, as you find out. Forms are for entities, not for collections. The bug is in your view and how you expect to handle index.

这篇关于如何为#&lt;Book::ActiveRecord_Relation:0x007fb709a6a8c0&gt;解决未定义的方法`to_key'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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