【问题标题】:Undefined method in my app. What's wrong?我的应用程序中未定义的方法。怎么了?
【发布时间】:2009-04-08 05:17:25
【问题描述】:

我尝试像这样在我的应用程序中阻止 IP 地址 - lifeonrails.org。我已经在 /lib 中有一个模块和模型banned_ip。

为什么我在views/banned_ips/index.html下面有这个错误?

===我的错误:===

Admin/banned_ips#index 中的 NoMethodError

显示第 9 行出现的 app/views/admin/banned_ips/index.html.erb:

undefined method `banned?' for "127.0.0.1":String

提取的源代码(第 9 行附近):

6:     <th>Last_ip</th>
7:     <th>Date</th>
8:   </tr>
9: <% if request.remote_ip.banned? == true %>banned<% else %>ok<% end %>
10: <% for banned_ip in @banned_ips %>
11:   <tr>
12:     <td><%=h banned_ip.first_ip %></td>

===模块 infrid.rb 在 /lib===

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

===模型禁止_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

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    您的问题是您试图在 String 上调用 banned?,而不是在您的 BannedIp 类上。您有两种解决方案。

    1. 将检查禁止IP的代码替换为BannedIp.banned?(request.remote_ip)
    2. 将方法修补到为您调用类方法的字符串类中,该方法是 rails stype 但可读性较差。

    这里需要这个。 (错误阻止代码块工作)

    class String
      def banned?
        BannedIp.banned?(self)
      end
    end
    

    【讨论】:

    【解决方案2】:

    request.remote_ip 将 IP 地址作为字符串返回,而字符串没有 banned? 方法。看起来你想要BannedIP.banned?(request.remote_ip)

    【讨论】:

    • 试试看,返回这个错误:Expected D:/IR/rails_apps/cinemato/app/models/banned_ip.rb to define BannedIP
    • 文件实际上在那个位置吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多