views:

76

answers:

2

I have 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 this:

class AppController < ApplicationController
  respond_to :json

  ...
  def start
    app.start
    format.json { render :json => {'ok'=>true} }
  end
end
A: 

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.

Josh Lindsey
A: 

This was very close. However, it does not automatically convert the hash to json. This was the final result:

class AppControlls < ApplicationController
  respond_to :json

  def start
    app.start
    respond_with( { :ok => true }.json )
  end
end

Thanks for the help.

Cory Gagliardi