Rails: How to store a complex RESTful application?

I always try to make my applications as RESTful as possible, but I recently started working on a complex project that requires several additional methods in my controller.

I usually achieve this by adding another one: assembly to the route, but this seems like a workaround and in the script I have 5.

Are there any guidelines for handling additional methods in the controller? They are usually simple shapes that update the model.

+1


a source to share


4 answers


Another solution I make is every time you find yourself creating an action that doesn't fit into RESTful actions like Search, so you might find that you are performing a search action on an article controller, which you could do instead to create this action, instead create a Search Controller and use RESTful actions inside that instead. There is no rule that says you need to use all actions, you may only need one or two, but it supports your RESTful API and your code.



It's not a hard and fast rule, but I certainly found it useful when trying to decide where to put things.

+6


a source


I think there is a little confusion in the Rails community about how and where to use the word RESTful. Strictly speaking, the only thing that can be RESTful is your web API (as mentioned by railsninja). The fact that application code that conforms to REST conventions (an application with a REST API) can usually be organized as a set of controllers (corresponding to resources) and methods in those controllers (which correspond to the four verbs of the HTTP protocol) is nothing more than a hint that how to keep your application clean and organized.

If we want to talk about a RESTful Rails application, we can't just talk about RESTful controllers - there is nothing RESTful about the controllers themselves. It is possible to have a complex web application with one controller (and many methods) that represents many resources and is excellently RESTful.



However, it's pretty good to add more methods to your controller. It is sometimes useful to extract some of these additional methods and create a completely new controller - do it anytime you feel good (rule of thumb): create a new controller anytime you can identify it with some self-contained resource, i.e. e. a resource that could only exist on its own). Sometimes, however, it would be silly to fetch some resource to another controller. Let's say you have a resource that has a status attribute. It makes sense to think of this status attribute as a resource in itself and perform at least one action on it (update), but it won't be useful to retrieve it on another controller.

+3


a source


There is Railscast on this topic , adding custom actions to another RESTful controller.

Edit: I see that you already mentioned the method in Railscast as a workaround. :-) I believe that the only other "clean" way to deal with this would be to add additional controllers as needed to support the required actions.

+1


a source


"I usually achieve this by adding one more: assembly to the route, but this seems like a workaround and in the script I have 5."

This sounds great to me. REST is not about crud operations and your concern seems to stem from the fact that you are doing more than basic crud operations, but there is nothing wrong with that.

+1


a source







All Articles