HonestHeuristic
HonestHeuristic

Reputation: 336

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.

Upvotes: 4

Views: 1412

Answers (2)

Roman Kovtunenko
Roman Kovtunenko

Reputation: 447

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

Upvotes: 5

rootatdarkstar
rootatdarkstar

Reputation: 1526

The file routes.rb contains special DSL for routes, but it's still ruby. So, did you try put your routes in simple condition?

# routes.rb

if Rails.env.development?
  # your special local routes definition
end

Upvotes: 1

Related Questions