通过POST自定义路由

时间:2013-07-15 07:38:36

标签: ruby-on-rails

我正在使用Rails 4.0

在routes.rb中我有代码

get "test/index"

工作正常。但是当我添加自定义路线时:

post "test/some_controller_action"

然后通过curl发送文件

curl -X POST -H "Content-type: application/json" -d @result.json http://localhost:3000/test/some_controller_action

我收到错误

Started POST "/test/some_controller_action" for 127.0.0.1 at 2013-07-15 14:28:11 +0700
ActionController::RoutingError (No route matches [POST] "/test/some_controller_action"):
  actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0) lib/rails/engine.rb:511:in `call'
  railties (4.0.0) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'

rake routes:

Prefix Verb URI Pattern                   Controller#Action
test_index GET  /test/index(.:format)      test#index
test_some_controller_action POST /test/some_controller_action(.:format) test#some_controller_action

该路线有什么问题?

2 个答案:

答案 0 :(得分:2)

尝试将此添加到您的路线并通过运行佣金路线查看您的路线

 match '/test/some_controller_action' => 'test#some_controller_action', :via => :post, :as => :some_controller_action

答案 1 :(得分:0)

我更喜欢这种格式,因为这提供了更好的可维护性。请注意,:on => :collection部分很重要,无论您喜欢哪种方式。

resources :test do
  post :some_controller_action, :on => :collection
end

HTH