在我的Rails 4应用程序中,我正在显示PDF文件的链接。用户应该能够单击链接并查看PDF。 我得到的是错误没有路线与[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'
答案 0 :(得分:0)
控制器动作
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