有人可以告诉我这段代码有什么问题吗...?

发布于 2024-07-26 18:16:57 字数 568 浏览 2 评论 0原文

我在控制器中有一段代码,它用 IMG 标签替换现有的 HTML。 代码如下:

 render :update do |page|
   page.replace_html "chart-div", "<img src=\"#{chart.chart_file}\"/>"  #chart.chart_file is a path
 end

无论出于何种原因,我不断收到以下错误:

  ActionController::RoutingError (No route matches "/public/charts/1_WEEKLY_ACTUAL_LINE.jpg" with {:method=>:get}):

知道为什么它假设我想路由到某个地方。 看来我必须在开头添加“public”才能正确创建文件,但是我必须删除“public”才能显示图像。 有什么想法吗? 是否有更标准的机制来处理动态创建的图像/项目?

最好的。

注意:请勿使用“上传”插件。 所有文件均由系统创建,没有上传。

I have a section of code in a controller that replaces existing HTML with an IMG tag. The code is as follows:

 render :update do |page|
   page.replace_html "chart-div", "<img src=\"#{chart.chart_file}\"/>"  #chart.chart_file is a path
 end

For whatever reason, I keep receiving the following error:

  ActionController::RoutingError (No route matches "/public/charts/1_WEEKLY_ACTUAL_LINE.jpg" with {:method=>:get}):

I have NO idea why it's assuming I want to route somewhere. It seems that I must have the "public" on the beginning in order for the file to be properly created, however I must remove "public" in order to display the image. Any thoughts? Is there a more standard mechanism by which to deal with dynamically created images/items?

Best.

NOTE: Please no "upload" plugins. All files are created by the system, there are no uploads.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2024-08-02 18:16:57

添加文件时,您会将其添加到文件系统,其位置为 RAILS_ROOT/public/charts/1_WEEKLY_ACTUAL_LINE.jpg

当您想要显示该文件时,您需要一个指向该文件的 URL。 存储在 public 目录中的文件可通过其相对于 public 目录的路径进行访问。

您可以尝试这样的操作:

class Chart < ActiveRecord::Base # or whatever the chart class is
  def chart_url
    chart_file.gsub(%r{^/public}, "")
  end
end

或者,您可以将 URL 存储在数据库中,然后执行以下操作:

class Chart < ActiveRecord::Base # or whatever the chart class is
  def chart_file
    "/public#{chart_url}"
  end
end

When adding the file, you are adding it to the file system, where it is located at RAILS_ROOT/public/charts/1_WEEKLY_ACTUAL_LINE.jpg.

When you want to display the file, you need a URL that points at it. Files stored in the public directory are accessed by their path relative to the public directory.

You might try something like this:

class Chart < ActiveRecord::Base # or whatever the chart class is
  def chart_url
    chart_file.gsub(%r{^/public}, "")
  end
end

Or, you can store the URL in the database, and do:

class Chart < ActiveRecord::Base # or whatever the chart class is
  def chart_file
    "/public#{chart_url}"
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文