为什么我在Rails中得到这个无路由匹配错误?

时间:2011-12-29 21:12:08

标签: ruby-on-rails ruby ruby-on-rails-3

我收到此错误:

Started POST "/admin/reports/2/backfill" for 127.0.0.1 at Thu Dec 29 16:09:00 -0500 2011

ActionController::RoutingError (No route matches "/admin/reports/2/backfill"):

当我发布此表单时:

<%=form_for @report, {:url => report_backfill_path(@report.id), :method => :post} do |f| %>
    <%=label_tag(:days, "number of days to backfill")%>
    <%=select_tag(:days, options_for_select((1..100).to_a.map{|i| [i,i]}))%>
    <%=f.submit "backfill!" %>
<% end %>

但是路线已经定义,请查看我的rake routes命令的输出:

report_backfill POST   /admin/reports/:report_id/backfill(.:format)    {:controller=>"reports", :action=>"backfill"}

包含此表单的页面呈现正常,只是当我发布它时,我收到了错误。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

因为您使用form_for @report@report是现有模型,所以它会在隐藏字段中生成PUT助手,这会使路由引擎认为它是PUT请求。

实际上,要遵循rails约定,因为您在现有报表上调用了一个额外的方法(即使副作用可能是创建事物),我会将路由更改为使用:put而不是{ {1}},并单独留下表格。