繁体   English   中英

Rails渲染不起作用

[英]Rails render doesn't work

我正在尝试从Rails控制器返回一个json值,但是奇怪的是它不起作用,并且总是需要html格式。 这是我的控制器方法:

  def result
        @command = params[:output]
        @result = []
        IO.popen("free -m") { |f| f.each { |e| @result << e } }
    rescue Errno::ENOENT
        @result << "No command found"

    render json: @result.to_json
  end

尝试访问此页面后,出现以下错误:MainController#result作为HTML处理

ActionView::MissingTemplate (Missing template main/result, application/result with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :haml]}. Searched in:
  * "/home/vyivrain/Documents/RubyProjects/Diploma/app/views"

为什么只需要html格式。 我也尝试执行response_to或不呈现任何内容或呈现纯文本,但结果相同。 顺便说一下,我的rails版本是4.1.5。 所以有点奇怪。 谢谢你的答案。

更改为此:

  def result
    begin
        @command = params[:output]
        @result = []
        IO.popen("free -m") { |f| f.each { |e| @result << e } }
    rescue Errno::ENOENT
        @result << "No command found"
    end

    render json: @result.to_json
  end

当您单击重定向到result操作的链接时,尝试指定:remote => true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM