ruby-on-rails - Rails 4单击链接打开PDF时没有路线匹配[GET]
问题描述
在我的 Rails 4 应用程序中,我显示了 PDF 文件的链接。用户应该能够单击链接并查看 PDF。我得到的是错误No route matches [GET]: "/assets/documents/pdf/..."
如果我需要在路由和控制器中添加一些东西,我不知道该怎么办?
我的控制器:
class SearchesController < ApplicationController
require 'net/http'
def index
if params[:search].present?
url = "http://localhost:3003/api/search?q=" + params[:search][:keyword]
uri = URI(url)
resp = Net::HTTP.get(uri)
@json_resp = JSON.parse(resp)
@result = @json_resp["docs"]
end
end
def search
end
end
风景:
<b>Pdf:</b> <%= link_to item["resource_name"], asset_path("/assets/documents/pdf/" + item["resource_name"]), :target => "_blank", :class => "links" %>
日志:
Started GET "/show_pdf/%2Fassets%2Fdocuments%2Fpdf%2F2018%2003%20Technical%20Conformance%20Guide%20v4.1.pdf" for 127.0.0.1 at 2018-08-07 14:34:11 +0200
ActionController::RoutingError (No route matches [GET] "/show_pdf/%2Fassets%2Fdocuments%2Fpdf%2F2018%2003%20Technical%20Conformance%20Guide%20v4.1.pdf"):
actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28: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.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.10) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.10) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.10) lib/rack/lock.rb:17:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.10) lib/rack/sendfile.rb:113:in `call'
railties (4.2.6) lib/rails/engine.rb:518:in `call'
railties (4.2.6) lib/rails/application.rb:165:in `call'
rack (1.6.10) lib/rack/lock.rb:17:in `call'
rack (1.6.10) lib/rack/content_length.rb:15:in `call'
rack (1.6.10) lib/rack/handler/webrick.rb:88:in `service'
/Users/jakublemiszewski/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
/Users/jakublemiszewski/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
/Users/jakublemiszewski/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'
解决方案
控制器动作
def inline_pdf
// read the params from the routes item["resource"]
and then read the full path of the file by
// File.expand_path(item["resource"])
send_data(File.expand_path(item["resource"], disposition: 'inline', type: 'application/pdf')
end
看法
<%= link_to item["resource_name"], inline_pdf_path(item["resource_name"])%>
路线
get "/inline_pdf/:name" => "controller_name#inline_pdf", as: :inline_pdf
推荐阅读
- c - 为什么显示带有负值的地址
- reactjs - 使用 Spring Security 的客户端基于 cookie 的身份验证
- tfs - 有没有办法显示最近的工作项活动?
- sql-server - 如何修复“?”附近的错误语法
- python - 我将如何验证 CSV 文件是否由某个字符分隔(在本例中为反引号 ( ` ) )
- mysql - 使用多个关键字在多列中进行 SQL 搜索
- python - 使用keras作为后端获取中间层的输出
- excel - 您可以在没有扩展名的情况下将变量引用/设置到工作簿吗?
- python - 如何启动 EC2 实例并按需运行任务
- oauth - Google Cloud OAuth 同意屏幕中的范围与调用 API 时请求的范围之间有什么关系?