我试图在我的应用中为上传的pdf文件实现下载功能。由于某种原因无法找到记录?如果我检查GET请求,我看到它尝试使用url结尾" /resumes/download.5"。 5是来自存储文件的记录id。 我缺少什么?如何在将来调试此类问题?由于某种原因,Byebug这次没有工作。
视图/恢复/ show.html.erb
<%= link_to "Download", resume_download_path(@resume) %>
resumes_controller.rb
class ResumesController < ApplicationController
around_filter :catch_not_found
before_action :find_resume, only: [ :show, :edit, :update, :destroy, :download ]
before_action :authenticate_user!
def show
end
def new
if @resume = current_user.resume
redirect_to @resume
else
@resume = Resume.new
end
end
def create
@resume = current_user.build_resume(resume_params)
if @resume.save
redirect_to @resume
else
render :new
end
end
def edit
end
def update
if @resume.update resume_params
redirect_to @resume, notice: "Your resume was successfully saved!"
else
render 'edit'
end
end
def destroy
@resume.destroy
redirect_to new_resume_path, notice: "Your resume was successfully deleted!"
end
def download
send_data @resume, type: "application/pdf", disposition: "attachment"
end
private
def resume_params
params.require(:resume).permit( :user_id, :download_file, :remove_download_file)
end
def find_resume
@resume = Resume.find(params[:id])
end
def catch_not_found
yield
rescue ActiveRecord::RecordNotFound
redirect_to(root_url, :notice => 'Record not found')
end
end
的routes.rb
Rails.application.routes.draw do
devise_for :users
root 'welcomes#index'
resources :resumes
get "resumes/download", as: "resume_download"
get '*path' => redirect('/')
end
点击下载链接时
Started GET "/resumes/download.5" for 77.8.13.5 at 2017-08-23 21:22:14 +0000
Cannot render console from 77.8.13.5! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ResumesController#show as
Parameters: {"id"=>"download"}
Resume Load (0.2ms) SELECT "resumes".* FROM "resumes" WHERE "resumes"."id" = ? LIMIT 1 [["id", 0]]
Redirected to https://rails-tutorial-martinbortowski.c9.io/
Completed 302 Found in 3ms (ActiveRecord: 0.2ms)
Started GET "/" for 77.8.13.5 at 2017-08-23 21:22:15 +0000
Cannot render console from 77.8.13.5! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by WelcomesController#index as HTML
Rendered welcomes/index.html.erb within layouts/application (0.2ms)
Completed 200 OK in 39ms (Views: 38.4ms | ActiveRecord: 0.0ms)
答案 0 :(得分:0)
你下载的链接似乎有误。
更改 routes.rb
get "resumes/download", as: "resume_download"
到
controller :resumes do
get "resumes/download/:id" => :show, as: :resume_download
end
<强>视图/恢复/ show.html.erb 强>
更改链接
<%= link_to "Download", resume_download_path(@resume) %>
到
<%= link_to "Download", resume_download_path(id: @resume) %>
答案 1 :(得分:0)
var ans = db.myTable.Where("it.myColumn LIKE 'text _'");
将为您提供一个惯用的正确REST路由:
resources :resumes do
get :download, on: :member
end
将您的链接更改为:
resumes/:id/download
请参阅Rails Routing from the Outside In - Adding More RESTful Actions。