Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you render hashes as JSON in Rails 3

I found how to render ActiveRecord objects in Rails 3, however I cannot figure out how to render any custom objects. I am writing an app without ActiveRecord. I tried doing something like this:

class AppController < ApplicationController
  respond_to :json

  ...
  def start
    app.start
    format.json { render :json => {'ok'=>true} }
  end
end
like image 387
Cory Gagliardi Avatar asked Feb 26 '23 11:02

Cory Gagliardi


1 Answers

When you specify a respond_to, then in your actions you would make a matching respond_with:

class AppControlls < ApplicationController
  respond_to :json

  def index
    hash = { :ok => true }
    respond_with(hash)
  end
end

It looks like you're conflating the old respond_to do |format| style blocks with the new respond_to, respond_with syntax. This edgerails.info post explains it nicely.

like image 125
Josh Lindsey Avatar answered Feb 28 '23 23:02

Josh Lindsey