【问题标题】:Display file contents from the given Rails DB显示来自给定 Rails DB 的文件内容
【发布时间】:2020-12-23 07:43:25
【问题描述】:

我可以使用<%= render file: "#{Rails.root}/filepath", layout: false %> 显示特定文件的内容,但是,我需要动态访问数据库中的每个文件并获取其文件路径。

index.html.erb视图文件中,我link_to articles_path,也就是这条路线article GET - /articles/:id(.:format) - articles#show。这意味着节目 URL 以articles/:id 结尾。我可以通过使用实例变量@article.title 和@article.id 来显示文件标题和ID。此外,我可以使用<%= link_to "View File", @article.file%> 获取下载链接,这是在显示视图页面<a href="/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--045b71b4dec3d3bfe6cec88145761e50106c0922/test2.txt">View File</a> 中下载文件的示例链接(当我检查页面时)。但是,我不想下载文件,只是在网页上显示内容(文本)。如何动态访问文件路径?

由于这个堆栈溢出问题:How to get the filename of uploaded file in rails contoller,我尝试使用 @filename = params[:file].original_filename(在 article_controller 的 show 方法中)。我认为它不起作用。

我也尝试了<%= cdata_section(File.read(@filename)) %>(在view/show.html.erb 文件中),但我得到一个未定义的错误:nil:NilClass 的未定义方法'original_filename'

代码:

#application_record.rb
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

#article.rb
class Article < ApplicationRecord
  has_one_attached :file
end

#articles_controller.rb
class ArticlesController < ApplicationController
  def index
    @articles = Article.all
  end
  
  def show
    @article = Article.find(params[:id])
  end
  
  def new
  end
  
  def create
    @article = Article.new(params.require(:article).permit(:title, :text, :file)) 
    @article.save

    redirect_to @article
  end
  
  def destroy
    @article = Article.find(params[:id])
    @article.destroy

    redirect_to articles_path
  end
  
  def article_params
    params.require(:article).permit(:title, :text, :file)
  end
end

我相信这些文件存储在本地 Rails DB 中的活动存储 blob 中。这是我的config/storage.yml;我们将文件存储在本地,因此文件位于我的应用根目录下的存储目录中的层次结构中

#config/storage.yml
test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

【问题讨论】:

  • params[:file] 是什么?您能否举一个显示 URL 的示例,因为通常这只是一个 id 被发送,但您试图像文件是参数一样调用它。您可能(没有看到)需要使用id 获取文件,然后根据存储方式添加路径。文件是如何存储的?这只是在本地您需要解决这个问题还是在托管在某处的应用程序中,如果是的话,文件实际托管在哪里?
  • @RockwellRice,我对我的帖子进行了更改以解决您的问题。

标签: ruby-on-rails


【解决方案1】:

通过链接呈现内容不是完整堆栈页面的选项。您要么必须使用 xhr 请求(ajax 请求)来获取内容,要么链接到呈现文件内容的视图。

假设您想保持简单,那么,有几件事要做。

命名是常规的,根据你的喜好调整。

为您的视图创建一条路线:

#config/routes.rb
get '/filecontents/:id', to: "articles#read"

然后,创建一个动作来渲染视图

#app/controllers/articles_controller.rb
def read
    @article = Article.find params[:id]
    path = method_to_get_your_path_from_article #this is your job
    @text = File.open(path).read # get the contents in a variable
end

现在,假设您的文件具有文本文件结构,您所要做的就是创建一个最小视图:

#views/articles/read.html.erb
<pre><%= @text %></pre>

这应该会让你继续前进。当然,您必须对其进行增强。

哦,别忘了将link_to设置为显示到您刚刚创建的路线

【讨论】:

  • 感谢您的帮助。不幸的是,我不确定应该为路径变量添加什么。我不知道如何正确获取文件路径。我尝试在终端中使用rake routes 搜索路线。我注意到,通过添加您建议的代码get '/filecontents/:id', to: "articles#read"GET - /filecontents/:id(.:format) - articles#read 被添加到路线列表中。但是,由于没有前缀(在 GET 之前),我不确定该调用什么。这是获取文件路径的正确方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多