0

我有一个名为 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] %>

有任何想法吗?

4

2 回答 2

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
于 2013-06-05T20:57:19.980 回答
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方法 from 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
于 2013-06-05T20:41:14.133 回答