0
因此,我有一個應用程序範圍的麪包屑幫助器方法,我在佈局中使用了所有視圖頁面。設置輔助方法返回值作爲實例變量
這裏是低於
- if request.fullpath != '/'
.row
.col-md-12
#breadcrumbs
= link_to 'home', '/'
%span »
- @BreadcrumbsHelper = breadcrumbs
- i = 0
- @BreadcrumbsHelper.each do |name, breadcrumb|
= link_to name, breadcrumb
- if i != (@BreadcrumbsHelper.count - 1)
%span »
- i = i + 1
方法從我的理解,在視圖中的變量應該是實例變量,而不是方法,但是聲明實例變量視圖並沒有真正似乎是有道理的,但我不確定還有其他方法可以接受,只是將它作爲方法調用,例如breadcrumbs.each do
?這甚至會工作嗎?最佳做法是什麼?
EDIT(助手,以防萬一它有助於):
module BreadcrumbsHelper
def breadcrumbs
current_path = request.fullpath
noparam_path = current_path.split('?')
path_parts = noparam_path[0].split('/')
path_parts.shift
counter = path_parts.size
new_paths = Hash.new
counter.times do
new_path = ""
path_name = ""
i = 0
path_parts.each do |part|
if i < counter
if new_path == ""
new_path = '/' + part
else
new_path = new_path + '/' + part
end
path_name = part
i = i + 1
end
end
counter = counter -1
#path functions
def routeValid(new_path)
route = "/" + new_path.gsub("_","/")
route_valid = true
begin
Rails.application.routes.recognize_path(route, :method => :get)
rescue
# error means that your route is not valid, so do something to remember that here
route_valid = false
end
return route_valid
end
def processPath(new_path, path_name, new_paths)
if routeValid(new_path) == true
#Optional Overrides
if path_name == "signup"
path_name = "sign up"
end
new_paths[path_name] = new_path
end
end
processPath(new_path, path_name, new_paths)
end
new_paths = Hash[new_paths.to_a.reverse]
return new_paths
end
end
大再次感謝皮特:) – Melbourne2991