【问题标题】:rails missing template from link_to and send_filerails 缺少来自 link_to 和 send_file 的模板
【发布时间】:2015-03-18 10:35:35
【问题描述】:

我正在尝试显示日志文件的内容,即 html 中的 txt 文件。 我收到“模板丢失错误”。

Missing template application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/home/Documents/workspace/demo2/app/views"

在我的控制器中我有

 class ApplicationController < ActionController::Base
  def index
  end
  def displaycontents
  send_file "/home/Documents/workspace/demo.txt", 
          :type => "text/html", 
          :disposition => 'inline'
  end
 end

在我的 routes.rb 我有

root 'application#index'

在我的 index.html.erb 我有

<!DOCTYPE html>
<html>
<head>
 <title>new view</title>
  </head>
  <body>
  <%= link_to "Log File", :controller=> :application,
    :action=>:displaycontents %>
  </body>
</html>

我已将控制器文件中的 index 操作保留为空,因此 index.html.erb 会默认呈现。

我已经按照另一个解决方案的建议重新启动了 rails 服务器。 并且 index.html.erb 文件存在于 demo2/app/views 中。

我使用的不同版本是 - 红宝石版本:1.8.23 - 红宝石版本:1.9.3 -导轨:4.0.2

【问题讨论】:

  • demo2/app/views?模板应放在 demo2/app/views/application 下。可以试试吗?
  • 什么时候出现这个错误?当您转到localhost:3000 或单击链接时?

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

在 send_file 之后添加and return 以避免渲染视图。

send_file("/home/Documents/workspace/demo.txt", :type => "text/html", :disposition => 'inline') and return

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2016-11-24
    相关资源
    最近更新 更多