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?
Something like:
def index @users = User.all respond_with @users do |format| format.json { render :layout => false, :text => @users.to_json } end end
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With