Rails PATCH方法而不是POST

时间:2017-02-06 14:42:39

标签: javascript ruby-on-rails ruby-on-rails-4 patch

我有以下代码

<%= form_for @campaign, url: brands_campaign_path(@campaign), method: :patch,  :html => {:multipart => true, :class => 'form-horizontal'} do |f| %>

生成以下内容

<form class="form-horizontal" id="edit_campaign_25" enctype="multipart/form-data" action="/campaigns/testing" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="&#x2713;" />
<input type="hidden" name="_method" value="patch" />
<input type="hidden" name="authenticity_token" value="Ybl3G26TZumVbbCew2EmlVxq4Dv2JZx5PAq1Q7nup3vp0GdkT72oIlV==" />

但是在服务器端我正在收到POST请求。我已经检查过jquery_ujs.js javascripts也正在加载到浏览器中。

佣金路线

 brands_campaigns GET    /campaigns(.:format)                                         brands/campaigns#index {:subdomain=>"brands"}
                                                      POST   /campaigns(.:format)                                         brands/campaigns#create {:subdomain=>"brands"}
                                  new_brands_campaign GET    /campaigns/new(.:format)                                     brands/campaigns#new {:subdomain=>"brands"}
                                 edit_brands_campaign GET    /campaigns/:id/edit(.:format)                                brands/campaigns#edit {:subdomain=>"brands"}
                                      brands_campaign GET    /campaigns/:id(.:format)                                     brands/campaigns#show {:subdomain=>"brands"}
                                                      PATCH  /campaigns/:id(.:format)                                     brands/campaigns#update {:subdomain=>"brands"}
                                                      PUT    /campaigns/:id(.:format)                                     brands/campaigns#update {:subdomain=>"brands"}
                                                      DELETE /campaigns/:id(.:format)                                     brands/campaigns#destroy {:subdomain=>"brands"}
                               brands_campaigns_posts GET    /campaigns/:id/posts(.:format)                               brands/campaigns#posts {:subdomain=>"brands"}
                             brands_campaigns_details GET    /campaigns/:id/details(.:format)                             brands/campaigns#details {:subdomain=>"brands"}
                         brands_campaigns_influencers GET    /campaigns/:id/influencers(.:format)                         brands/campaigns#influencers {:subdomain=>"brands"}
                 brands_campaigns_submit_for_approval GET    /campaigns/:id/submit_for_approval(.:format)                 brands/campaigns#submit_for_approval {:subdomain=>"brands"}
                    brands_campaigns_pending_to_draft GET    /campaigns/:id/pending_to_draft(.:format)                    brands/campaigns#pending_to_draft {:subdomain=>"brands"}
                              brands_campaigns_cancel GET    /campaigns/:id/cancel(.:format)                              brands/campaigns#cancel {:subdomain=>"brands"}
                         brands_campaigns_add_credits GET    /campaigns/:id/add_credits(.:format)                         brands/campaigns#add_credits {:subdomain=>"brands"}

日志

ActionController::RoutingError (No route matches [POST] "/campaigns/testing/edit"):   actionpack (4.2.5.2) 
lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'   rollbar (2.14.0)
lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'   web-console (2.3.0) 
lib/web_console/middleware.rb:20:in `block in call'   web-console (2.3.0) 
lib/web_console/middleware.rb:18:in `catch'   web-console (2.3.0) 
lib/web_console/middleware.rb:18:in `call'   actionpack (4.2.5.2) 
lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'   railties (4.2.5.2)
lib/rails/rack/logger.rb:38:in `call_app'  railties (4.2.5.2) 
lib/rails/rack/logger.rb:20:in `block in call'   activesupport (4.2.5.2) 
lib/active_support/tagged_logging.rb:68:in `block in tagged'   activesupport (4.2.5.2)
lib/active_support/tagged_logging.rb:26:in `tagged'   activesupport (4.2.5.2) 
lib/active_support/tagged_logging.rb:68:in `tagged'   railties (4.2.5.2) 
lib/rails/rack/logger.rb:20:in `call'   actionpack (4.2.5.2) 
lib/action_dispatch/middleware/request_id.rb:21:in `call'   rack (1.6.5) 
lib/rack/methodoverride.rb:22:in `call'   rack (1.6.5) 
lib/rack/runtime.rb:18:in `call'   activesupport (4.2.5.2) 
lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'   rack (1.6.5)
lib/rack/lock.rb:17:in `call'   actionpack (4.2.5.2) 
lib/action_dispatch/middleware/static.rb:116:in `call'   rack (1.6.5) 
lib/rack/sendfile.rb:113:in `call'   railties (4.2.5.2) 
lib/rails/engine.rb:518:in `call'   railties (4.2.5.2) 
lib/rails/application.rb:165:in `call'   rack (1.6.5) 
lib/rack/tempfile_reaper.rb:15:in `call'   rack (1.6.5) 
lib/rack/lint.rb:49:in `_call'   rack (1.6.5)
lib/rack/lint.rb:37:in `call'   rack (1.6.5) 
lib/rack/showexceptions.rb:24:in `call'   rack (1.6.5) 
lib/rack/commonlogger.rb:33:in `call'   sinatra (1.4.7) 
lib/sinatra/base.rb:219:in `call'   rack (1.6.5) 
lib/rack/chunked.rb:54:in `call'   rack (1.6.5) 
lib/rack/content_length.rb:15:in `call'   unicorn (5.2.0) 
lib/unicorn/http_server.rb:562:in `process_client'   unicorn (5.2.0) 
lib/unicorn/http_server.rb:658:in `worker_loop'   unicorn (5.2.0) 
lib/unicorn/http_server.rb:508:in `spawn_missing_workers'   unicorn (5.2.0)
lib/unicorn/http_server.rb:132:in `start'   unicorn (5.2.0) 
bin/unicorn:126:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.1/bin/unicorn:23:in `load'   
/usr/local/rvm/gems/ruby-2.3.1/bin/unicorn:23:in `<main>'   
/usr/local/rvm/gems/ruby-2.3.1@global/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.3.1@global/bin/ruby_executable_hooks:15:in `<main>'

应该像在Firefox上一样点击什么路线

Started PATCH "/campaigns/testing" for 106.51.30.182 at 2017-02-06 11:18:02 -0500

1 个答案:

答案 0 :(得分:3)

来自文档:

  

提交表单时使用的方法,通常是“获取”或   “后”。如果使用“patch”,“put”,“delete”或其他动词,则隐藏   添加名为_method的输入以模拟帖子上的动词。

来源:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

如您问题中的代码段所示 <input type="hidden" name="_method" value="patch" /> 模拟patch

上的post方法