[英]rails app downloading documents instead of displaying
I am creating a document upload form for my rails app, everything works fine, except when I try and display the.txt document that has just been uploaded, the document is downloaded instead?我正在为我的 Rails 应用程序创建文档上传表单,一切正常,除了当我尝试显示刚刚上传的 .txt 文档时,该文档被下载了吗?
These are the two lines of code running on my show.html.erb file这些是在我的 show.html.erb 文件中运行的两行代码
<%= link_to 'View file', @document.file %> <%= link_to '查看文件', @document.file %>
<%= link_to 'Download', @document.file, download: '' %> <%= link_to '下载', @document.file, 下载: '' %>
both the View file and Download buttons are downloading the file.查看文件和下载按钮都在下载文件。
How do I go about displaying the file?我如何 go 关于显示文件?
This is the application controller:这是应用程序 controller:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
added_attrs = [:username, :email, :password, :password_confirmation, :remember_me]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
end
end
And this is the documents controller. The files being uploaded are called documents:这是文件 controller。上传的文件称为文件:
class DocumentsController < ApplicationController
before_action :set_document, only: [:show, :edit, :update, :destroy]
# GET /documents
# GET /documents.json
def index
@documents = Document.all
end
# GET /documents/1
# GET /documents/1.json
def show
end
# GET /documents/new
def new
@document = Document.new
end
# GET /documents/1/edit
def edit
end
# POST /documents
# POST /documents.json
def create
@document = Document.new(document_params)
respond_to do |format|
if @document.save
format.html { redirect_to @document, notice: 'Document was successfully created.' }
format.json { render :show, status: :created, location: @document }
else
format.html { render :new }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /documents/1
# PATCH/PUT /documents/1.json
def update
respond_to do |format|
if @document.update(document_params)
format.html { redirect_to @document, notice: 'Document was successfully updated.' }
format.json { render :show, status: :ok, location: @document }
else
format.html { render :edit }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
end
end
# DELETE /documents/1
# DELETE /documents/1.json
def destroy
@document.destroy
respond_to do |format|
format.html { redirect_to documents_url, notice: 'Document was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_document
@document = Document.find(params[:id])
end
# Only allow a list of trusted parameters through.
def document_params
params.require(:document).permit(:title, :description, :file)
end
end
You should check that you are sending the file with the option disposition: :inline
in your rails controller. The default value of that option is 'attachment'
.您应该检查您发送的文件是否带有选项
disposition: :inline
in your rails controller。该选项的默认值为'attachment'
。
For example in your controller action send your file for example with:例如,在您的 controller 操作中发送您的文件,例如:
send_file document.file,
:type => "application/pdf",
:file_name => document.file.name,
:disposition => 'inline'
If you want your link to be opened in a separate tab you can just add target: '_blank'
to your link options.如果您希望您的链接在单独的选项卡中打开,您只需将
target: '_blank'
添加到您的链接选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.