HonestHeuristic HonestHeuristic - 1 month ago 21
Ruby Question

Create rails routes only for localhost

I'd like certain rails routes to only be accessible from localhost. In other words if you attempted to access that url from a non localhost connection you would be given a response equivalent to the route not existing.

Optimally some way of specifying routes as local in the routes.rb itself would be the cleanest solution but if there is some way to filter the request later at the controller level for example thats okay too.

Answer

If you want to specify that these urls exist only in a development environment, you can do just simple:

if Rails.env.development?
  #your routes
end

But if your server in development mode is being accessed by others and you want to specify that these routes exist for localhost only then you can add constraint for domain:

if Rails.env.development?
  resources :users, constraints: { domain: 'localhost' }
end
Comments