Topic: REST and AJAX

I am just starting to get my head around rest and I am loving it. It makes everything simple. The one thing I am trying to figure out is where ajax fits in to rest. I know I can just set render :layout => false on my forms for new and edit and add some condition logic for an xhr, but what about certain ajax features that require their own actions like autocompletion. I know that not everything fits into rest, but I am just trying to find the most restful solution.

http://www.brianthecoder.com

Rails, Javascript, Actionscript and More.

Re: REST and AJAX

Good question. I have wondered this as well. If anyone knows of a good solution, please respond! I don't have much first hand experience with this, but here's my opinion anyway. wink

The first question to ask yourself is it performing a CRUD operation. If so it should go in the REST controller and should use respond_to to separate the javascript behavior. In some sense the auto-completion should call the "index" action of a REST controller which manages the resource which will be displayed. If you are already using the index action, consider making another :collection action in the controller.

There are still other AJAXy goodness that doesn't fit into CRUD. Validations is an example. If you want to validate-as-they-type, then where will you put this? IMO this should go in a separate controller. For one you may need to share similar behavior across controllers, so moving it into a separate controller makes sense to me. You could move it into its own "validations" controller or just have a more generic "ajax" controller where you place these little things. I wouldn't bother trying to make this part RESTful.

Railscasts - Free Ruby on Rails Screencasts

Re: REST and AJAX

But if you make a validation controller how do you make it so they can't navigate to it and get errors?

http://www.brianthecoder.com

Rails, Javascript, Actionscript and More.

Re: REST and AJAX

Don't provide links to it. smile

Other than that, if they want to type in the URL to reach the page then there's no stopping them. You can't (securely) tell if the HTTP request was from javascript on a browser or someone accessing it in other ways.

You would have the same problems in a non-RESTful design.

Why is this a concern though?

Railscasts - Free Ruby on Rails Screencasts

Re: REST and AJAX

not really. I was just wondering if there was a way to block it so that even if they typed it in they couldn't get to it. Just trying to minimize the possibility of things going wrong.

http://www.brianthecoder.com

Rails, Javascript, Actionscript and More.

Re: REST and AJAX

You could do this:

raise "Should only access through AJAX" unless request.xhr?

This may prevent some users from triggering the action by typing in the URL directly.

Railscasts - Free Ruby on Rails Screencasts