Rails 3中的路由问题:ActionController :: RoutingError(没有路由匹配......)

时间:2011-01-10 19:10:34

标签: routing ruby-on-rails-3

我创建了一个名为“post”的自定义RESTful操作。它在TransactionsController中显示为名为post的(公共)方法。

resources :transactions do
  member :post do
    post :post
  end
end

我的表格配置如下:

<form action="/transactions/25/post">
   ...
   <input id="transaction_submit" commit="commit" type="submit" value="Post">
</form>

当我点击“发布”按钮时,我的服务器收到:

POST "/transactions/25/post"

我希望这可以在我的TransactionController中调用“post”方法,但我得到一个路由错误

ActionController::RoutingError (No route matches "/transactions/25/post"):

有什么想法吗?感谢。

詹姆斯

1 个答案:

答案 0 :(得分:8)

终于找到了一个解决方案,问题是form_for添加了隐藏的_method字段,其值为"put",因为事务已经存在,为了规避这个问题,我必须做以下内容:

<%= form_for @transaction, :url => post_transaction_path(@transaction), :html => { :method => :post } do |form| %>

至少为我解决了这个问题,请参阅https://rails.lighthouseapp.com/projects/8994/tickets/4884-routing-error-for-restful-resource-under-namespace以获取进一步的参考资料