【发布时间】:2017-10-20 16:23:09
【问题描述】:
我在 Rails 中使用form_for 生成一个表单,如下所示:
# Controller
def new
@species = Specie.new
render partial: 'maintainers/species/new'
end
# In _new.html.erb
<%= render 'maintainers/species/form', species: @species %>
# In maintainers/species/form
<%= form_for(species, html: {remote: true, id: 'species_form'}) do |f| %>
...
<% end %>
但我收到此错误:
No route matches {:action=>"show", :controller=>"species"} missing required keys: [:id]
会发生什么?
【问题讨论】:
-
点击提交后会出现这种情况吗?
-
不,这发生在部分充电,创建表单时。
-
您的代码中有错字:
@species = Specie.new应该是@species = Species.new。不知道是不是这个问题。 -
实例化 Species 时不会生成 id。那是你的问题
-
@TomAranda 我已经用 Scaffold 生成了这个,并且 Specie 对象在其他地方工作正常。