Rails渲染不起作用

时间:2014-11-12 11:03:08

标签: ruby-on-rails json render

我试图从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处理#结果为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。这有点奇怪。谢谢你的答案。

2 个答案:

答案 0 :(得分:1)

将其更改为:

  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

答案 1 :(得分:0)

点击重定向到:remote => true操作的链接

,尝试指定result