Oleksandr Verhun
Oleksandr Verhun

Reputation: 854

Rails render doesn't work

I'm trying to return a json value from rails controller, but strangely it doesn't work and always wants html format. Here's my controller method:

  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

After I try to reach to this page, I get this kind of error: Processing by MainController#result as 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"

Somewhy it only wants html format. I also tried to do respond_to or render nothing or render plain, but it gives the same result. By the way my rails version is 4.1.5. So it's kinda strange. Thx for your answers.

Upvotes: 0

Views: 688

Answers (2)

Choco
Choco

Reputation: 1064

try to specify :remote => true when you click on the link that redirects to result action

Upvotes: 0

Ahmed Al Hafoudh
Ahmed Al Hafoudh

Reputation: 8429

Change it to this:

  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

Upvotes: 1

Related Questions