为什么form_for rails正在生成动作:'show'?

时间:2017-10-20 16:23:09

标签: ruby-on-rails form-for

我在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]

可能会发生什么?

1 个答案:

答案 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中显示操作。