2016-08-31 3 views
0

ビュー内にボタンがあるとします。それをクリックすると、ファイルのダウンロードが開始されます。Rails 'button_to'が 'send_file'メソッドと連携していません

私が持っている私の見解で

:homes_controller.rbでroutes.rbを

resources :homes, only: [:update] do 
    member do 
    get 'download_file' 
    end 
end 

<%= link_to 'Download File', download_file_home_path %> 

def download_file 
    send_file "path/to/file.pdf" 
end 

私はリンクをクリックすると、すべてが完璧に動作し、ダウンロードを開始します。しかし、私はリンクの代わりにボタンが必要です。私はbutton_toメソッドを使用する場合、それは失敗し、私は次のエラーを取得する:

ActionController::RoutingError (No route matches [POST] "/source_machines/4/download_report"): 
    actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app' 
    railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call' 
    activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged' 
    activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged' 
    activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged' 
    railties (4.2.5) lib/rails/rack/logger.rb:20:in `call' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    rack (1.6.4) lib/rack/methodoverride.rb:22:in `call' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    rack (1.6.4) lib/rack/runtime.rb:18:in `call' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    rack-rewrite (1.5.1) lib/rack/rewrite.rb:24:in `call' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    rack (1.6.4) lib/rack/lock.rb:17:in `call' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    rack (1.6.4) lib/rack/sendfile.rb:113:in `call' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    railties (4.2.5) lib/rails/engine.rb:518:in `call' 
    railties (4.2.5) lib/rails/application.rb:165:in `call' 
    railties (4.2.5) lib/rails/railtie.rb:194:in `public_send' 
    railties (4.2.5) lib/rails/railtie.rb:194:in `method_missing' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    rack (1.6.4) lib/rack/deflater.rb:35:in `call' 
    newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call' 
    rack (1.6.4) lib/rack/content_length.rb:15:in `call' 
    puma (3.6.0) lib/puma/configuration.rb:225:in `call' 
    puma (3.6.0) lib/puma/server.rb:578:in `handle_request' 
    puma (3.6.0) lib/puma/server.rb:415:in `process_client' 
    puma (3.6.0) lib/puma/server.rb:275:in `block in run' 
    puma (3.6.0) lib/puma/thread_pool.rb:116:in `call' 
    puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread' 
    logging (2.1.0) lib/logging/diagnostic_context.rb:450:in `call' 
    logging (2.1.0) lib/logging/diagnostic_context.rb:450:in `block in create_with_logging_context' 

それはlink_toで動作しないとbutton_to方法では動作しません、と私は行うことができますが、それはbutton_toで動作させるために、なぜ私の質問は@prashantのコメントに続いて

+1

アクションのためのあなたのルートをGETリクエストのために作成され、あなたのリンクは非常にGETリクエストを送信しているためですそれは働いていますが、button_toはどのルートとも一致しないPOST要求としてリクエストを送信しています。 – prashant

+0

'メソッド:' get ''を追加しブームを起こしました。どうもありがとうございます。 –

+1

を取得するためにbutton_toを使用する場合は、これを次のように使用してください: '<%= link_to 'Download File'、download_file_home_path、method::get%>'それが動作します。 – prashant

答えて

0

This is because your route for the action is created for GET request and your link is sending GET request so it working.But button_to is sending request as POST request which is not matching to any route.

だからmethod: 'get'問題を修正追加します。

ビューは次のようになります。

<%= button_to 'Download Report', download_file_home_path, method: 'get' %> 
1

[OK]を、それを試してみてください。

<%= button_to 'your_button_name', your_path, method: 'get' %> 
関連する問題