Why do I need to work harder to make my Rails application fit into a RESTful architecture? Why do I need to work harder to make my Rails application fit into a RESTful architecture? ruby ruby

Why do I need to work harder to make my Rails application fit into a RESTful architecture?


I would treat search as a special case of index. Both actions return a collection of resources. The request parameters should specify things like page, limit, sort order, and search query.

For example:

/resources/index # normal index/resources/index?query=foo # search for 'foo'

And in resources_controller:

before_filter :do_some_preprocessing_on_parametersdef index  @resources = Resource.find_by_param(@preprocessed_params)end

As for index_full and search_by_name, you might look at splitting your current controller into two. There's a smell about what you've described.

Having said that, you're absolutely right that there's no point in forcing your app to user restful routes when it doesn't deliver anything over /:controller/:action/:id. To make the decision, look how frequently you're using the restful resource route helpers in forms and links. If you're not using them, I wouldn't bother with it.


If I go beyond the standard CRUD actions with my models, I normally just add the methods as required. Searching is something I add to many controllers, but not every one, so I add it and maintain the routes normally:

map.resources :events, :collection => { :search => :get }

Moving these actions to an entirely separate controller might keep some of your controllers RESTful, but I find that keeping them in context is far more useful.


REST does not specify that you can't have additional views. No real world application is going to be able use only the supplied actions; this is why you can add your own actions.

REST is about being able to make stateless calls to the server. Your search action is stateless each time as the data so far is supplied back, correct? Your alternate display action is also stateless, just a different view.

As to if they should be manual routes or a new controller, that depends on how distinct the activity is. Your alternate view, if it provides a full set of CRUD (create, read, update, delete) operations would do well to be in a new controller. If you only have an alternate view to the data, I would just add an alternate view action.

In other words, it doesn't sound like your application is failing to be RESTful, it is more an issue of realizing that the automatically generated feature set is a starting point, not a conclusion.