Download rails应用程序下载文档而不是显示
我正在为我的rails应用程序创建一个文档上传表单,一切正常,除了当我尝试显示刚刚上传的.txt文档时,文档被下载了 这是在我的show.html.erb文件上运行的两行代码 “查看文件”和“下载”按钮都在下载文件 如何显示文件 这是应用程序控制器:Download rails应用程序下载文档而不是显示,download,rails-activestorage,railsapps,Download,Rails Activestorage,Railsapps,我正在为我的rails应用程序创建一个文档上传表单,一切正常,除了当我尝试显示刚刚上传的.txt文档时,文档被下载了 这是在我的show.html.erb文件上运行的两行代码 “查看文件”和“下载”按钮都在下载文件 如何显示文件 这是应用程序控制器: class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_con
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
class ApplicationController
这是文件管理员。正在上载的文件称为文档:
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
类文档控制器
您应该检查是否在rails控制器中使用选项disposition::inline
发送文件。该选项的默认值是“附件”
例如,在控制器操作中,发送文件,例如:
send_file document.file,
:type => "application/pdf",
:file_name => document.file.name,
:disposition => 'inline'
如果您想在单独的选项卡中打开链接,只需在链接选项中添加
目标:''u blank'
。我已添加到原始问题中。如果你可以看一下控制器,因为我有点迷路了。你试过把send\u文件添加到get方法吗?这应该能奏效。然后,您可以在中更改链接:
。这将由控制器的get方法处理,get方法将返回带有处置内联的文件。请尝试重新编辑您的帖子,以便更易于阅读