在视图中,假设我有一个按钮。如果我点击它,它应该触发文件下载。
在我看来,我有:
<%= link_to 'Download File', download_file_home_path %>
在routes.rb
中resources :homes, only: [:update] do
member do
get 'download_file'
end
end
在homes_controller.rb
中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
一起使用
答案 0 :(得分:1)
好的,试一试:
<%= button_to 'your_button_name', your_path, method: 'get' %>
答案 1 :(得分:0)
按照@prashant的评论,
这是因为您为GET请求创建了操作的路由,并且您的链接正在发送GET请求以使其正常工作。但是button_to正在发送请求作为POST请求,该请求与任何路由都不匹配。
因此添加method: 'get'
解决了问题。
视图应如下所示:
<%= button_to 'Download Report', download_file_home_path, method: 'get' %>