[英]RESTful routes in Rails for generating reports

I am working with a Rails application that uses RESTful routes for handling it's resources. I am now creating a reports controller that will generate reports in HTML, XML, CSV, etc. There will be several different reports available for generation, depending on the parameters sent to the controller.


Is it overkill to use REST for this reports controller as it's not an actual resource that will be saved and then available for editing or deletion? Using RESTful would create a lot of routes that I will never need to use.


Would it be a better practice to define a custom route instead of going RESTful? Such as having a single generate action in the controller that generates the report and outputs it in the specified format?


map.connect 'reports', :controller => 'reports', :action => 'generate'

3 个解决方案



I would do it in non RESTful way. There is no need for it to be RESTful. Even @jdl answer isn't RESTful, because it contains only one action show. In this case reports aren't resources that can be created, edited or deleted. I would add this kind of routes:


map.report 'reports/:id', :controller => 'reports', :action => 'generate'
map.report_with_format 'reports/:id.:format', :controller => 'reports', :action => 'generate'
map.reports 'reports', :controller => 'reports', :action => 'index'

Using named routes instead of connect will give you some nice url helpers like reports_path etc.




Your way is fine, or if you prefer to stick with the RESTful routes you can pick and choose the ones you want.


map.resources :reports, :only => [:show]



Sorry resurrecting such old post.


According to RESTful Web Services, you don't need to provide write-actions so your service can be considered RESTful.

根據RESTful Web Services,您不需要提供寫入操作,因此可以將您的服務視為RESTful。

As far as I know, you need to design your service using ROA (Resource-Oriented Architecture) and that's it.


That being said, @jdl answer was correct and RESTful. :)

話雖如此,@ jdl的答案是正確的,RESTful。 :)



粤ICP备14056181号  © 2014-2021 ITdaan.com