我在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]
可能会发生什么?
答案 0 :(得分:0)
您需要发出POST请求才能调用create action:
# In maintainers/species/form
<%= form_for(species, html: {remote: true, , method: :post, id: 'species_form'}) do |f| %>
...
<% end %>
注意method: :post
参数。
默认情况下, form_for 将发出GET请求,该请求映射为在Rails中显示操作。