Rails 3路由约束,允许某些IP

时间:2012-06-14 18:43:13

标签: ruby-on-rails-3 routes ip constraints

我在Rails应用程序的路由文件中有以下代码:

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

这成功地将123.123.123.123 IP地址路由到约束块内的控制器。

向约束块添加另一个IP地址的最佳方法是什么,所以我可以将两个或更多个IP地址路由到相同的控制器?简单地添加如下的其他内容似乎不起作用:

constraints :ip => "123.123.123.123, 232.232.232.232" do

任何指针都将不胜感激!

1 个答案:

答案 0 :(得分:2)

也许是这样的?

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

同样想到这个答案 - > Rails 3 Routing Constraint and Regex

另一个猜测...... xD