views:

202

answers:

4

I'm making my first real Rails app, and am learning about REST architecture and how it fits into Rails apps. My controllers don't really seem to map logically to an individual resource, so it's tough for me to implement strict REST. For instance, I have a "catalog", "checkout" and "admin" controller, not a "products", "categories", "orders" and "users" controller, although those are the main resources being used in those controllers. Each of the controllers does more than just REST actions on a resource however, all of them pull from multiple models and service multiple views.

Am I missing a design/architectural practice that would make REST work better here? Namespaced controllers perhaps, with non-REST actions in the top-level controller only, and REST actions in the sub-controllers? Seems like I would run into some DRY violations there...

+1  A: 

If this is the way you want to structure your app, forget about REST and just map the routes you need. If you don't actually want to manage resources why bother?

nasmorn
I guess I don't really need to worry about an accessible API for a shopping cart site. I do like the url and path helpers that come along with map.resources, however. Thanks for your reply.
+1  A: 

REST is great for resources. It's really painful to map a checkout procedure as a restful resource and not worth the effort, personally, I just have 4/5 actions in my checkout controller and that seems to do the job.

If you want the helpers, you can map named routes

checkout_delivery_path

map.checkout_delivery '/checkout/delivery', :controller => "checkout", :action => "delivery"
Omar Qureshi
A: 

If you look at the HTTP spec here you will see that one thing that POST can be used for is:

Providing a block of data, such as the result of submitting a
        form, to a data-handling process;

So, you can create a controller that is a "data-handling process" and do a POST to it without violating any REST constraints. If you name the resource with a noun you can make URL feel a bit more RESTful, but it really is not not necessary. e.g.

POST /MyStore/CheckoutGirl

As for your Catalog, I don't see why that doesn't map easily. GET /MyStore/Catalog/Item/2324 GET /MyStore/Catalog/SaleItems

The admin usually maps to users, roles, etc. There is nothing new there.

Darrel Miller
A: 

Stop trying to shoehorn every aspect of REST into every application. REST is an architecture, and it's not suitable for all purposes. If what you need is some RPC action, then use some other form of RPC, not the resource-centric principles of REST.

Also, the URL naming schemes are totally irrelevant to whether your application is RESTful or not. That's not what REST is about. They're nice to have, but just do what makes sense for your application.

Wahnfrieden