使用 form_for 时出现未定义方法 'to_key' 错误
Undefined method 'to_key' error using form_for
在我的 Rails 应用程序中尝试使用 form_for 时出现以下错误:
undefined method `to_key' for #<Table::ActiveRecord_Relation:0x8a09ca8>
我的 config/routes.rb 是:
root 'welcome#index'
post 'foo', as: 'foo', to: 'welcome#index'
控制器是:
class WelcomeController < ApplicationController
def index
@tables = Table.all
end
def test
@tables = Table.all
end
end
而 welcome/index.html.erb 视图是:
<p>
<%= form_for @tables, :url => foo_path do |t| %>
<%= t.text_area :name %>
<% end %>
</p>
我已尝试执行文档中建议的 url 解决方法,但我仍然遇到同样的错误。
有谁知道我做错了什么?我想更多地了解这个错误,以便更好地处理它。
根据您的代码,index
正在返回一个集合。但是,您的视图试图为其定义一种形式。这不太可能成功。
表单是针对对象的,不是针对集合的。
也许你可以做类似的事情
def new
@table = Table.new
end
并在 new.html.erb
<%= form_for @table do |f| %>
...
<% end %>
如果您想坚持使用 index.html.erb
表格。然后你必须为索引操作编辑你的路由,并且在控制器中它应该用于创建一个新对象。
def index
@table = Table.new
end
希望对您有所帮助!
我看到你的代码有 3 个不是真的
那么作为 RESFUL 标准:
index 动作总是通过 get 动作进行,所以在路由文件中你应该再次定义相同的:
root "wellcome#index"
get "foo", to: "wellcome#index", as: :foo
form_for 通常与模型对象一起使用但不收集,因为您使用 @tables,如果模型对象未保存到数据库 form_for 用于创建 1对象到数据库,否则 form_for 使用更新该对象
- 如果你想在索引操作中创建表单,你可以关注我:
def index
@tables = Table.all
@table = Table.new
end
index.html.erb 文件
<%= form_for @table 做 |f| %>
<%= f.label :名称 %><br>
<%= f.text_field :名称 %><br>
<%= f.submit %>
<% 结束 %>
您需要创建 tables_controller 来处理从表单发送到服务器的请求。你 运行:
rails g controller tables
在 table_controller.rb 你写的一样:
定义创建
@table = Table.new table_params
如果@table.save
redirect_to root_path, 注意: "success"
别的
redirect_to root_path,警报:"fail"
结尾
结束</li>
</ul>
<p>私人
定义 table_params
params.require(:table).permit :name
结尾
以便。结尾。祝你有美好的一天!
在我的 Rails 应用程序中尝试使用 form_for 时出现以下错误:
undefined method `to_key' for #<Table::ActiveRecord_Relation:0x8a09ca8>
我的 config/routes.rb 是:
root 'welcome#index'
post 'foo', as: 'foo', to: 'welcome#index'
控制器是:
class WelcomeController < ApplicationController
def index
@tables = Table.all
end
def test
@tables = Table.all
end
end
而 welcome/index.html.erb 视图是:
<p>
<%= form_for @tables, :url => foo_path do |t| %>
<%= t.text_area :name %>
<% end %>
</p>
我已尝试执行文档中建议的 url 解决方法,但我仍然遇到同样的错误。
有谁知道我做错了什么?我想更多地了解这个错误,以便更好地处理它。
根据您的代码,index
正在返回一个集合。但是,您的视图试图为其定义一种形式。这不太可能成功。
表单是针对对象的,不是针对集合的。
也许你可以做类似的事情
def new
@table = Table.new
end
并在 new.html.erb
<%= form_for @table do |f| %>
...
<% end %>
如果您想坚持使用 index.html.erb
表格。然后你必须为索引操作编辑你的路由,并且在控制器中它应该用于创建一个新对象。
def index
@table = Table.new
end
希望对您有所帮助!
我看到你的代码有 3 个不是真的
那么作为 RESFUL 标准:
index 动作总是通过 get 动作进行,所以在路由文件中你应该再次定义相同的:
root "wellcome#index" get "foo", to: "wellcome#index", as: :foo
form_for 通常与模型对象一起使用但不收集,因为您使用 @tables,如果模型对象未保存到数据库 form_for 用于创建 1对象到数据库,否则 form_for 使用更新该对象
- 如果你想在索引操作中创建表单,你可以关注我:
def index @tables = Table.all @table = Table.new end
index.html.erb 文件
<%= form_for @table 做 |f| %> <%= f.label :名称 %><br> <%= f.text_field :名称 %><br> <%= f.submit %> <% 结束 %>
您需要创建 tables_controller 来处理从表单发送到服务器的请求。你 运行:rails g controller tables
在 table_controller.rb 你写的一样:定义创建 @table = Table.new table_params 如果@table.save redirect_to root_path, 注意: "success" 别的 redirect_to root_path,警报:"fail" 结尾 结束</li> </ul> <p>私人 定义 table_params params.require(:table).permit :name 结尾
以便。结尾。祝你有美好的一天!