# Restful Rails with Ajax

**URL:** <https://www.ruby-forum.com/t/restful-rails-with-ajax/153794>\
**Category:** Rails\
**Created:** [November 21, 2008, 5:16am UTC](https://www.ruby-forum.com/t/restful-rails-with-ajax/153794 "2008-11-21T05:16:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-black](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joe-black](https://www.ruby-forum.com/u/joe-black)\
**Post date:** [November 21, 2008, 5:16am UTC](https://www.ruby-forum.com/t/restful-rails-with-ajax/153794/1 "2008-11-21T05:16:43Z")

</div>

What is the proper way to handle Ajax requests restfully?

def index  
@user = User.all  
respond\_to do |format|  
format.html # Normal requests  
format.js # Should I use this?  
format.rjs # Something like this?  
end  
end

I am unsure how to accomplish this.

Do I need something like this possibly?

if request.xhr?  
#Do something  
else  
respond\_to do |format|  
#Normal stuff  
end  
end

What is the proper thing to do?

---

<div class="post-metadata">

**Author:** ![joe-black](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joe-black](https://www.ruby-forum.com/u/joe-black)\
**Post date:** [November 21, 2008, 6:27am UTC](https://www.ruby-forum.com/t/restful-rails-with-ajax/153794/2 "2008-11-21T06:27:02Z")

</div>

You had it about right:

# /app/controllers/users\_controller.rb

def index  
@user = User.all  
respond\_to do |format|  
format.html # Normal requests  
format.js # XHR Requests  
end  
end

# /app/views/users/index.js.rjs

page[‘some\_element’].hide
