Regex used rails routes - ruby-on-rails

What regex is used in Rails routes such that it works cross environments. I am trying to setup a middleware such that requests coming to /:id:/show/details does a reverse proxy to wordpress.com/:id, was wondering if I can use something Rails has already built-in for this use case

You can always do something like this:
get '/:id/show/details', to: redirect{|params| "https://wordpress.com/mypage/#{params[:id]}"}

Related

Query which controller will be used for a URL

Question
I'm looking for something in the rails console or similar where I can call get_controller_for_path('/some/path/') and it will return the corresponding controller.
Background
I have a rails project with a lot of routes. I'm investigating a routing problem and want to confirm that a given URL will match a specific route.
I can use bundle exec rake routes to view the list of routes, but that still requires my human eyes to parse the hundreds (thousands?) of routes and figure out what's going on.
Here you go: Rails parse url to hash(Routes)
That says it's Rails.application.routes.recognize_path "/accounts/1"
You can also use assert_routing in your tests. If you don't have tests, now is a very good time to add some. And you can read the source of assert_routing, and assert_recognizes, to see what they do.

Does Backbone.js have something like 'rake routes' from Ruby on Rails?

Is there an easy way to see all the routes my Backbone.js application has built while it's running?
Ruby on Rails has rake routes, which shows things like this:
unicorns GET /unicorns(.:format) unicorns#index
POST /unicorns(.:format) unicorns#create
new_unicorn GET /unicorns/new(.:format) unicorns#new
edit_unicorn GET /unicorns/:id/edit(.:format) unicorns#edit
unicorn GET /unicorns/:id(.:format) unicorns#show
PUT /unicorns/:id(.:format) unicorns#update
DELETE /unicorns/:id(.:format) unicorns#destroy
Does Backbone have an equivalent?
You can always check a router prototype/instance routes property to get all statically declared routes. So if you're not generating them programmatically, you'll get the cleanest output as so:
console.log( router.routes );
If you're using this.route() to dynamically add routes, then you should check on the Backbone.History object, this will be the complete list of routes used inside your app; although the output is a little bit messiers (routes are compiled to regexp, etc).
console.log( Backbone.history.handlers );
Note that this property ain't documented, so there's no garantee it will be kept in future version of Backbone. I'd only use it for debugging purpose.

Implementing hypermedia-driven API with Grape (or Sinatra)

I'm trying to implement a hypermedia-driven API using Grape mounted directly on top of Rack. Grape supports presenters ("entities") which
seem to be the proper place for providing all related hypermedia.
If I had Rails router available, I could simply pick route by its ears and toss it into my presenter logic. For example (ROAR approach):
link :self do
article_url(self)
end
But Grape itself doesn't provide easy access to routes, as they have no names or aliases akin to article_url.
Has anybody encountered a similar problem with Grape or Sinatra? Is there a clean and simple way of exposing resource links?
This is possible, but not quite as simply as the Rails url helpers.
From https://github.com/intridea/grape#describing-and-inspecting-an-api:
TwitterAPI::versions # yields [ 'v1', 'v2' ]
TwitterAPI::routes # yields an array of Grape::Route objects
TwitterAPI::routes[0].route_version # yields 'v1'
TwitterAPI::routes[0].route_description # etc.

How do I write a full path in a controller in Rails 3?

I need to write the full path so need to know what the rails_root domain is. How do I do that? For example:
string = "{RAILS_ROOT}/vendors/#{#vendor.id}"
What is the equivalent of "RAILS_ROOT" to give me what the full domain is for my application? So that in development it would subsstitute localhost:3000 and on my heroku site the right full domain?
You should always avoid, if possible, hard-coding your path, because it is less flexible and more prone to result in broken links in the future. Plus, you can use Rails routing, which is an elegant way to generate everything cohesively in Rails without any need to create the composite parts yourself.
If you have your routes set up properly, you should be able to call:
link_to "View vendor", vendor_url(#vendor.id)
Vendor_url(#vendor.id) in Rails gives you your full URL, which you can then contain in your string variable. Here's how to generate the routes needed for the above:
# in routes.rb
resources :vendors
Try:
File.realpath(RAILS_ROOT)
You could access the request object. request.host_with_port would give you the hostname and port. request.protocol will give you the protocol (http:// or https://). request.fullpath will give you the path with query params.

porting .net => ruby, can I respond to urls ending in .aspx?

My .net application has a url that ends with .aspx like:
www.example.com/users/find.aspx?userid=234234
I don't want to re-write the url, I want to keep it as is.
Is it possible for a Rails 3 application (nginx, passenger) to respond to this request using a customized route?
Yes - you need to update your routes file (config/routes.rb).
See:
http://guides.rubyonrails.org/routing.html#non-resourceful-routes
Sure, just add a route to config/routes.rb. In Rails 3 that would like:
match 'users/find.aspx' => 'users#find'

Resources