Rails application without routes.rb file - ruby-on-rails

I am new to RoR and I have an API rails application that does not have routes.rb file.
I know it works but I don't know how.
What are the possible approaches(exept routes.rb file) to route requests?

In rails it's not necessary to have a routes.rb file. Any file/files inside config directory might have route destination like this:
Rails.application.routes.draw do
# Your routes here ..
end

Related

Redirection in Rails 4 routes

My site used to have a mobile view here:
https://www.example.com/m/home
We have deprecated the mobile views and now I need a simple way to trim the /m/ off the URL so that the request proceeds to the correct page.
Example:
https://www.example.com/m/about => https://www.example.com/about
https://www.example.com/m/user/:id => https://www.example.com/user/:id
I'm hoping to solve this in the Rails routing without having to introduce a new controller action or meddle with nginx. I have 100+ routes. Thanks in advance.
Rails version: 4.2
There is a redirection module (also documented in the guide).
Something like :
get '/m/about', to: redirect('/about')
get '/m/user/:id', to: redirect('/user/%{id}')
Which you can combine with route globbing for a generic solution :
get '/m/*path', to: redirect('/%{path}')
How about just refactor your routes a bit:
Eg: Previous routes.rb
resources :users
# ...
Now, it becomes:
['m', ''].each do |sc|
scope sc do
resources :users
# ...
end
end

How to avoid caching of routes with DHH's monkey patch about distributing routes in multiple files

I'm using Rails 3.1 and DHH's monkey patch about distributing routes in multiple files:
https://gist.github.com/2492118
So when I change routes in one of the routes of the routes directory I have to deactivate a routes sub file like config/routes/users.rb:
MyApplication::Application.routes.draw do
#draw :users
end
Request a page and activate the routes sub file.
MyApplication::Application.routes.draw do
draw :users
end
After I request a page I can finally see the new user route changes.
Does anyone know a solution on how to avoid this workaround and see the change in route sub files immediately?

Splitting Routes File Into Multiple Files

I'm working w/ a Rails 3 application and I want to split up the routes into separate files depending on the subdomain. Right now I have this in my routes.rb file:
Skateparks::Application.routes.draw do
constraints(:subdomain => 'api') do
load 'routes/api.rb'
end
end
And In my routes/api.rb file I have:
resources :skateparks
This doesn't seem to work though because if I run rake routes I get
undefined method `resources' for main:Object
Also, if I try to navigate to http://0.0.0.0:3000/ I get:
Routing Error
No route matches "/"
In Rails 3.2, config.paths is now a hash, so #sunkencity's solution can be modified to:
# config/application.rb
config.paths["config/routes"] << File.join(Rails.root, "config/routes/fooroutes.rb")
Sunkencity's answer seems to be identical to the following link, but for completeness' sake: https://rails-bestpractices.com/posts/2011/05/04/split-route-namespaces-into-different-files/
Note that routes defined later will override routes defined earlier. However, if you use something like
config.paths.config.routes.concat(
Dir[Rails.root.join('config/routes/*.rb')])
you don't know in what order the files will be read. So use
config.paths.config.routes.concat(
Dir[Rails.root.join('config/routes/*.rb')].sort)
instead, so you at least know they will be in alphabetical order.
Add the route file to the app route loading path:
# config/application.rb
config.paths.config.routes << File.join(Rails.root, "config/routes/fooroutes.rb")
Wrap your other route file in a block like this.
#config/routes/fooroutes.rb
Rails.application.routes.draw do |map|
match 'FOO' => 'foo/bar'
end
Works for me in rails 3.0
We used this in our app:
config.paths['config/routes'] = Dir["config/routes/*.rb"]
If you try to access config.paths['config/routes'] normally, it returns the relative path to config/routes.rb, so by doing the above you're giving it relative paths to all of the files in your routes folder and removing the reference to config/routes.rb

Sinatra & Rails 3 routes issue

I have just setup Sinatra v1.1.0 inside my rails (v3.0.1) app. But I can't invoke any routes that are more than 1 level deep, meaning this works - http://localhost/customer/3,
but this one does not work - http://localhost/customer/3/edit and I get a "Routing Error"
Here's the Sinatra object
class CustomerApp < Sinatra::Base
# this works
get "/customer/:id" do
"Hello Customer"
end
# this does NOT work
get "/customer/:id/edit" do
"Hello Customer"
end
end
This is what I have in my rails routes.rb file -
match '/customer/(:string)' => CustomerApp
I am guessing I need some magic in the routes file? What could be the problem?
In your routes file, you can specify the mapping this way:
mount CustomerApp, :at => '/customer'
Now, inside your sinatra application, you can specify your routes without the /customer part.
Dont't forget to require your sinatra application somewhere (you can do it directly in the route file)
You need to add an additional route to match the different URL:
match '/customer/(:string)/edit' => CustomerApp

I can't serve static images on Ruby on Rails

I'm trying to access:
http://localhost:3000/images/Header.png
but I keep getting this error:
Routing Error
No route matches "/images/Header.png" with {:method=>:get}
And here are my routes:
ActionController::Routing::Routes.draw do |map|
map.resources :worker_antpile_statuses
map.resources :worker_antpiles
map.resources :antcolonies
map.resources :antpiles
map.resources :clients
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
Static assets need to go in the /public folder or else you'll get the routing error. For a static image at http://localhost:3000/images/Header.png you want to place Header.png in RAILS_ROOT/public/images
When given any url, Rails will check for a file existing at the path from RAILS_ROOT/public directory before attempting to match any routes.
For example when a Rails server receives a request for http://localhost:3000/users/1 it will attempt to send the contents of RAILS_ROOT/public/users/1. If no file exists, the route matching routine kicks in.
The last two lines of your routes.rb are catch-all routes that will match any previously unmatched URLs.
Comment out those two lines and it will work, which is what you want to do when you use RESTful routing in all your application.
See this excellent guide for all you need to know about Rails routes.

Resources