DOM手术导致路由神秘失败

时间:2018-07-12 12:04:33

标签: ruby-on-rails

我正在使用Javascript更改表单标签并为其赋予新的操作字符串。但是从我所看到的一切来看,操作字符串是正确的。但是,Rails给了我一个路由错误。寻找另一双眼睛...

以下是路线:

                   Prefix Verb   URI Pattern                                                                              Controller#Action
     content_translations POST   /contents/:content_id/translations(.:format)                                             translations#create
  new_content_translation GET    /contents/:content_id/translations/new(.:format)                                         translations#new
 edit_content_translation GET    /contents/:content_id/translations/:id/edit(.:format)                                    translations#edit
      content_translation PATCH  /contents/:content_id/translations/:id(.:format)                                         translations#update
                          PUT    /contents/:content_id/translations/:id(.:format)                                         translations#update
                          DELETE /contents/:content_id/translations/:id(.:format)                                         translations#destroy
                 contents GET    /contents(.:format)                                                                      contents#index
                          POST   /contents(.:format)                                                                      contents#create
              new_content GET    /contents/new(.:format)                                                                  contents#new
             edit_content GET    /contents/:id/edit(.:format)                                                             contents#edit
                  content GET    /contents/:id(.:format)                                                                  contents#show
                          PATCH  /contents/:id(.:format)                                                                  contents#update
                          PUT    /contents/:id(.:format)                                                                  contents#update
                          DELETE /contents/:id(.:format)                                                                  contents#destroy
                     root GET    /                                                                                        contents#index
       rails_service_blob GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
       rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
     rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create
~/m/rails52demo1 (master *=) 

这是html:

<form data-target="content.modalForm" action="/contents/3" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="ra5nE1ZAv0+jC9GoU7UDRIaBTJpEFky7lf2Yu0GkVDjs/QZrjl1RH6fPVsS/4YzSO1A0XcgSBs8RkTtNm8OYcA==">
        <div class="field">
          <label class="label" for="content_text">Enter Original Text</label>
            <div class="control">
              <textarea class="textarea" name="content[text]" id="content_text"></textarea>
            </div>
        </div>

        <div class="field">
          <label class="label" for="content_from_lang">Language of original</label>
            <div class="select">
              <select name="content[from_lang]" id="content_from_lang"><option value="en">English</option>
<option value="fr">French</option>
<option value="es">Spanish</option>
<option value="nl">Dutch</option>
<option value="ar">Arabic</option>
<option value="is">Icelandic</option>
<option value="ja">Japanese</option>
<option value="it">Italian</option>
<option value="iw">Hebrew</option></select>
            </div>
        </div>
        <footer class="modal-card-foot">
          <div class="actions control">
            <input type="submit" name="commit" value="OK" class="button is-success" data-disable-with="OK">
          </div>
        </footer>
</form>    

这是错误:

Started POST "/contents/3" for 127.0.0.1 at 2018-07-12 07:57:45 -0400

    ActionController::RoutingError (No route matches [POST] "/contents/3"):

    actionpack (5.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:65:in `call'
    web-console (3.6.2) lib/web_console/middleware.rb:135:in `call_app'
    web-console (3.6.2) lib/web_console/middleware.rb:30:in `block in call'
    web-console (3.6.2) lib/web_console/middleware.rb:20:in `catch'
    web-console (3.6.2) lib/web_console/middleware.rb:20:in `call'
    actionpack (5.2.0) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
    railties (5.2.0) lib/rails/rack/logger.rb:38:in `call_app'
    railties (5.2.0) lib/rails/rack/logger.rb:26:in `block in call'
    activesupport (5.2.0) lib/active_support/tagged_logging.rb:71:in `block in tagged'
    activesupport (5.2.0) lib/active_support/tagged_logging.rb:28:in `tagged'
    activesupport (5.2.0) lib/active_support/tagged_logging.rb:71:in `tagged'
    railties (5.2.0) lib/rails/rack/logger.rb:26:in `call'
    sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
    actionpack (5.2.0) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
    actionpack (5.2.0) lib/action_dispatch/middleware/request_id.rb:27:in `call'
    rack (2.0.5) lib/rack/method_override.rb:22:in `call'
    rack (2.0.5) lib/rack/runtime.rb:22:in `call'
    activesupport (5.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
    actionpack (5.2.0) lib/action_dispatch/middleware/executor.rb:14:in `call'
    actionpack (5.2.0) lib/action_dispatch/middleware/static.rb:127:in `call'
    rack (2.0.5) lib/rack/sendfile.rb:111:in `call'
    webpacker (3.5.5) lib/webpacker/dev_server_proxy.rb:22:in `perform_request'
    rack-proxy (0.6.4) lib/rack/proxy.rb:57:in `call'
    railties (5.2.0) lib/rails/engine.rb:524:in `call'
    puma (3.11.4) lib/puma/configuration.rb:225:in `call'
    puma (3.11.4) lib/puma/server.rb:632:in `handle_request'
    puma (3.11.4) lib/puma/server.rb:446:in `process_client'
    puma (3.11.4) lib/puma/server.rb:306:in `block in run'
    puma (3.11.4) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

2 个答案:

答案 0 :(得分:0)

事实证明,使用Rails路由,当您要更新模型时,表单标签显示method =“ post”,但还有一个附加标签:<input name="_method" type="hidden" value="patch" />如果没有,确实会得到所示的路由错误。因此,正确的方法是:

<form data-target="content.modalForm" action="/contents/1" accept-charset="UTF-8" method="post">
       <input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="XUQvzwi2ciBJTFanlHlhGrYPEYfHu/O+oziHQy6r6iAcF0630KuccE2I0ct4Le6MC95pQEu/uconVCS19MwmaA==">
        <div class="field">
          <label class="label" for="content_text">Enter Original Text</label>
            <div class="control">
              <textarea class="textarea" name="content[text]" id="content_text"></textarea>
            </div>
        </div>

        <div class="field">
          <label class="label" for="content_from_lang">Language of original</label>
            <div class="select">
              <select name="content[from_lang]" id="content_from_lang"><option value="en">English</option>
<option value="fr">French</option>
<option value="es">Spanish</option>
<option value="nl">Dutch</option>
<option value="ar">Arabic</option>
<option value="is">Icelandic</option>
<option value="ja">Japanese</option>
<option value="it">Italian</option>
<option value="iw">Hebrew</option></select>
            </div>
        </div>
        <footer class="modal-card-foot">
          <div class="actions control">
            <input type="submit" name="commit" value="OK" class="button is-success" data-disable-with="OK">
          </div>
        </footer>
</form>

答案 1 :(得分:0)

您的操作中具有get方法时,表单中的方法名称将为post。