问题描述
我遇到了未定义方法 `to_key' 的问题
这是我的 books_controller.rb
class BooksController
和我的索引页如下.
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
.
这篇关于如何为#<Book::ActiveRecord_Relation:0x007fb709a6a8c0>解决未定义的方法`to_key'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!