Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Rails 3 routes, how do you only allow a requests from 127.0.0.1?

I'm writing an app where several of the routes should only be accessible from localhost. It looks like this is possible with the new routing system.

http://www.railsdispatch.com/posts/rails-3-makes-life-better

This has examples of restricting routes based on IP address, and setting up an IP address blacklist for your routes, but I'm interested in a whitelist with just one IP address.

It would be cool if something like this worked:

get "/posts" => "posts#show", :constraints => {:ip => '127.0.0.1'}

But it didn't. Am I just missing the right syntax?

like image 814
micah Avatar asked Jun 16 '10 00:06

micah


People also ask

What does as do in Rails routes?

The :as option creates a named path. You can then call this path in your controllers and views (e.g. redirect_to things_path ). This isn't very useful for the root path (as it already named root ), but is very useful for new routes you add.

What is the difference between member and collection in Rails route?

Considering the same case, the two terms can be differentiated in a simple way as :member is used when a route has a unique field :id or :slug and :collection is used when there is no unique field required in the route.

What is the difference between resources and resource in Rails?

Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.

What is RESTful route in Rails?

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.


1 Answers

you can do this

get "/posts" => "posts#show", :constraints => {:ip => /127.0.0.1/}

or this

constraints(:ip => /127.0.0.1/) do
  get "/posts" => "posts#show"
end
like image 127
NicBa Avatar answered Oct 28 '22 18:10

NicBa