I have a Rails route that takes stock ticker symbols as the :id
feeds/AMZN
will return a page for Amazonfeeds/AMZN.csv
will return a CSV representation of the same data.But I also need to accomodate stocks like VIA.B (Viacom) so that both of these routes work:
feeds/VIA.B (html) feeds/VIA.B.csv (csv)
Is this possible? How would I set the routing up?
In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as. map.resources :photos. creates seven different routes in your application: HTTP verb.
rake routes will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you're trying to get familiar with.
We have to define the routes for those actions which are defined as methods in the BookController class. Open routes. rb file in library/config/ directory and edit it with the following content. The routes.
I ran into this while patching the RubyGems API recently (trying to access the flickr.rb
using the API (/api/v1/gems/flickr.rb.json
) was not working).
The trick was to supply the route with a regexp to handle the :id
parameter, and then specify valid :format
. Keep in mind that the :id
regexp needs to be "lazy" (must end with a question mark), otherwise it will eat the .csv
and assume that it's part of the id. The following example would allow JSON, CSV, XML, and YAML formats for an id with a period in it:
resources :feeds, :id => /[A-Za-z0-9\.]+?/, :format => /json|csv|xml|yaml/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With