undefined方法`[]'表示nil:NilClass表示应用程序助手中的方法

时间:2013-06-05 20:34:54

标签: ruby-on-rails ruby ruby-on-rails-3

我有一个名为crumbs的方法,它说的是未定义的方法。我从sinatra应用程序导入代码并尝试使用rails应用程序。

这是方法:

def crumbs
primary, secondary = current_link
return {
    primary:   { icon: menu[primary.to_sym][:primary][:icon], label: menu[primary.to_sym][:primary][:label]},
    secondary: {
        icon:  menu[primary.to_sym][:items][secondary.to_sym][:icon],
        label: menu[primary.to_sym][:items][secondary.to_sym][:label]
    }
}
end

以下是该方法中包含的其他方法:

 def menu
  return {
    dashboard: {
        primary: { link: "dashboard", icon: "icon-dashboard", label: "Dashboard" },
        items: {
            dashboard: { icon: "icon-dashboard", label: "Dashboard" }
        }
    },
    ui_lab: {
        primary: { link: "buttons", icon: "icon-beaker", label: "UI Lab" },
        items: {
            buttons: { icon: "icon-hand-up",       label: "Buttons" },
            general: { icon: "icon-beaker",        label: "General elements" },
            icons:   { icon: "icon-info-sign",     label: "Icons"},
            grid:    { icon: "icon-th-large",      label: "Grid"},
            tables:  { icon: "icon-table",         label: "Tables"},
            widgets: { icon: "icon-plus-sign-alt", label: "Widgets"},
        }
    },
    forms: {
        primary: { link: "forms", icon: "icon-edit", label: "Forms" },
        items: {
            forms: { icon: "icon-edit", label: "Form Elements" }
        }
    },
    charts: {
        primary: { link: "charts", icon: "icon-bar-chart", label: "Charts"},
        items: {
            charts: { icon: "icon-bar-chart", label: "Charts"}
        }
    },
    other: {
        primary: { link: "wizard", icon: "icon-link", label: "Others"},
        items: {
            wizard: { icon: "icon-magic", label: "Wizard" },
            login: { icon: "icon-user", label: "Login Page" },
            sign_up: { icon: "icon-user", label: "Sign Up Page" },
            full_calendar: { icon: "icon-calendar", label: "Full Calendar" },
            error404: { icon: "icon-ban-circle", label: "Error 404 page" },
        }
    }
}
end

以及这一个:

def current_link
root_path = "/"
current_route = request.url.split("/") # pages/dashboard/stats.html -> ['dashboard', 'stats.html']
return current_route.compact.first, current_route.compact.split(".").first
end

在视图中,这就是所谓的:

      <i class="<%= crumbs[:secondary][:icon] %>"></i>
        <%= crumbs[:secondary][:label] %>

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

追捕这些内容的一种方法是用[]调用替换:fetch来电。当它爆炸时,它会返回一个KeyError,告诉你哪个键丢失了:

> mystery = { a: 1, b: 2, c: 3 }
=> {:a=>1, :b=>2, :c=>3}

> mystery[:d][:q]
NoMethodError: undefined method `[]' for nil:NilClass

> mystery.fetch(:d).fetch(:q)
KeyError: key not found: :d

答案 1 :(得分:1)

只需为nil定义缺少的方法:

def nil.[] x
  puts "You hit the jackpot!!! You win #{x}!!!"
  # Or come up with the behavior according to your own needs.
end

除了笑话,你的问题没有提供足够的信息来确定你的真正问题。您的一个或多个哈希值似乎不包含搜索到的密钥,然后您尝试向其发送#[]方法。尝试Object#try中的ActiveSupport方法,或写一下:

def crumbs
  primary, secondary = current_link
  return {
    primary: { icon: ( menu[primary.to_sym][:primary][:icon] rescue nil ),
               label: ( menu[primary.to_sym][:primary][:label]} rescue nil ),
    secondary:
      { icon: ( menu[primary.to_sym][:items][secondary.to_sym][:icon] rescue nil ),
        label: ( menu[primary.to_sym][:items][secondary.to_sym][:label] rescue nil )
      }
    }
end