Ruby on rails 我的应用程序中未定义的方法。发生了什么?
我试着像这样阻止我的应用程序中的Ip地址-。我在/lib和model\u ip中已经有了一个模块 为什么我在下面的views/banked_ips/index.html中出现此错误 ==我的错误:=== 管理/禁止索引中的命名错误 显示app/views/admin/banked_ips/index.html.erb,其中第9行出现: 第9行附近的提取源: ==模块infrid.rb在/lib中===Ruby on rails 我的应用程序中未定义的方法。发生了什么?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我试着像这样阻止我的应用程序中的Ip地址-。我在/lib和model\u ip中已经有了一个模块 为什么我在下面的views/banked_ips/index.html中出现此错误 ==我的错误:=== 管理/禁止索引中的命名错误 显示app/views/admin/banked_ips/index.html.erb,其中第9行出现: 第9行附近的提取源: ==模块infrid.rb在/lib中=== module Infrid class IPAddress include Com
module Infrid
class IPAddress
include Comparable
def initialize(address)
@address = address
end
def split
@address.split(‘.‘).map {|s| s.to_i }
end
def <=>(other)
split <=> other.split
end
def to_s
@address
end
end
end
===禁用的型号\u ip:===
class BannedIp < ActiveRecord::Base
@banned_ips # hash of ips and masks
validates_presence_of :first_ip, :message =>"first address is needed"
validates_presence_of :last_ip, :message =>"last address is needed"
validates_format_of :first_ip, :with => REG_IP, :message => "is invalid (must be x.x.x.x where x is 0-255)", :if => Proc.new {|ar| !ar.first_ip.blank? }
validates_format_of :last_ip, :with => REG_IP, :message => "is invalid (must be x.x.x.x where x is 0-255)", :if => Proc.new {|ar| !ar.last_ip.blank? }
def self.banned?(ip)
reload_banned_ips if @banned_ips.nil?
begin
ip = Infrid::IPAddress.new(ip)
@banned_ips.each { |b|
return true if ip.between?(b[0], b[1])
}
rescue
logger.info "IP FORMAT ERROR"
return true
end
false
end
def self.banned_ips
reload_banned_ips if @banned_ips.nil?
@banned_ips.collect {|b| b[0].to_s + ".." + b[1].to_s }.join"\n"
end
#keeps a cache of all banned ip ranges
def self.reload_banned_ips
r = connection.select_all("select first_ip, last_ip from banned_ips")
if !r
@banned_ips=[]
end
@banned_ips = r.map {|item| [Infrid::IPAddress.new(item["first_ip"]),Infrid::IPAddress.new(item["last_ip"])] }
end
end
该request.remote_ip以字符串形式返回ip地址,并且字符串没有禁用的?方法看起来您想要BannedIP.Banding?request.remote_ip。您的问题是您试图呼叫banned?在绳子上,而不是在你的课堂上。你有两个解决方案 将用于检查禁用ip的代码替换为banndip.banding?request.remote_ip 将一个方法修补到string类,该类为您调用class方法,该方法在rails stype中,但可读性较差。 我需要这个。错误阻止代码块工作
class String
def banned?
BannedIp.banned?(self)
end
end
请尝试,返回此错误:预期D:/IR/rails_apps/cinemato/app/models/banned_ip.rb来定义BannedIP文件是否实际位于该位置?谢谢,BannedIP.banned?request.remote_ip在使用“ip”而不是“ip”写入BannedIP时有效。谢谢,BannedIP.banned?request.remote_ip在使用“ip”写入BannedIP时有效。
class BannedIp < ActiveRecord::Base
@banned_ips # hash of ips and masks
validates_presence_of :first_ip, :message =>"first address is needed"
validates_presence_of :last_ip, :message =>"last address is needed"
validates_format_of :first_ip, :with => REG_IP, :message => "is invalid (must be x.x.x.x where x is 0-255)", :if => Proc.new {|ar| !ar.first_ip.blank? }
validates_format_of :last_ip, :with => REG_IP, :message => "is invalid (must be x.x.x.x where x is 0-255)", :if => Proc.new {|ar| !ar.last_ip.blank? }
def self.banned?(ip)
reload_banned_ips if @banned_ips.nil?
begin
ip = Infrid::IPAddress.new(ip)
@banned_ips.each { |b|
return true if ip.between?(b[0], b[1])
}
rescue
logger.info "IP FORMAT ERROR"
return true
end
false
end
def self.banned_ips
reload_banned_ips if @banned_ips.nil?
@banned_ips.collect {|b| b[0].to_s + ".." + b[1].to_s }.join"\n"
end
#keeps a cache of all banned ip ranges
def self.reload_banned_ips
r = connection.select_all("select first_ip, last_ip from banned_ips")
if !r
@banned_ips=[]
end
@banned_ips = r.map {|item| [Infrid::IPAddress.new(item["first_ip"]),Infrid::IPAddress.new(item["last_ip"])] }
end
end
class String
def banned?
BannedIp.banned?(self)
end
end