views:

64

answers:

2

I have this setup:

class UsersController < InheritedResources::Base
  respond_to :html, :js, :xml, :json

  def index
    @users = User.all
    respond_with(@users)
  end
end

Now I am trying to make it so, if params[:format] =~ /(js|json)/, render :layout => false, :text => @users.to_json. How do I do that with respond_with or respond_to and inherited_resources?

+2  A: 

Something like:

def index
  @users = User.all
  respond_with @users do |format|
    format.json { render :layout => false, :text => @users.to_json }
  end
end
Yannis
A: 

Or to prevent you having to hardcode responses for each format in each action.

If you have no layouts for any of the actions in this controller it would be nicer to do:

class UsersController < InheritedResources::Base
  respond_to :html, :js, :xml, :json
  layout nil

  def index
    @users = User.all
    respond_with(@users)
  end
end
Jeremy