dannymcc
dannymcc

Reputation: 3814

Rails 3 route contstraint, allow certain IP's

I have the following code in my Rails application's routes file:

MyApp::Application.routes.draw do
constraints :ip => "123.123.123.123" do
    resources :sheets
    resources :consults
    resources :clinicals
    ...
 end
 end

This successfully routes the 123.123.123.123 IP address to the controllers within the constraint block.

What is the best way of adding another IP address to the constraint block, so I can route two or maybe more to the same controllers? Simply adding another like follows doesn't seem to work:

constraints :ip => "123.123.123.123, 232.232.232.232" do

Any pointers would be appreciated!

Upvotes: 0

Views: 928

Answers (1)

MurifoX
MurifoX

Reputation: 15089

Maybe something like this?

constraints :ip => IpsRouting.new

class IpsRouting
  def initialize
    @ips = ["123.123.123.123", "345.345.345.345"]
  end

  def matches?(request)
    return false if @ips.include?(request.remote_ip)
    true
  end
end

Same thought of this answer -> Rails 3 Routing Constraint and Regex

And another guess... xD

Upvotes: 3

Related Questions