Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify ":layout => false" in Rails' respond_with?

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?

like image 329
Lance Avatar asked Oct 06 '10 21:10

Lance


2 Answers

Something like:

def index   @users = User.all   respond_with @users do |format|     format.json { render :layout => false, :text => @users.to_json }   end end 
like image 144
Yannis Avatar answered Sep 28 '22 04:09

Yannis


Assuming you need JSON for an Ajax request

class UsersController < InheritedResources::Base   respond_to :html, :js, :xml, :json    def index     @users = User.all     respond_with(@users, :layout => !request.xhr? )   end end 

This seems like the cleanest solution to me.

like image 29
Anthony Bishopric Avatar answered Sep 28 '22 05:09

Anthony Bishopric