我正在尝试构建一个块助手,但我似乎无法找到一种方法来访问current_page?来自班上。
我的帮助文件如下所示:
class NavList
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
def header(title)
content_tag :li, title, class: 'nav-header'
end
def link(title, path, opts={})
content_tag :li, link_to(title, path), class: opts[:class]
end
end
def nav_list(&block)
new_block = Proc.new do
helper = NavList.new
block.call(helper)
end
content_tag :ul, capture(&new_block), class: 'nav nav-list'
end
我可以通过
使用帮助器<%= nav_list do |nl| %>
<%= nl.header 'Location' %>
<%= nl.link 'Basic Information', url_for(@department), class: current_page?(@departments) ? 'active' : '' %>
<%= nl.link 'Employees', department_users_path(@department) %>
<% end %>
但我想做的是不必经常接受那个活跃的课程。 所以我想做这样的事情
def link(title, path, opts={})
css_class = 'inactive'
css_class = 'active' if current_page?(path)
content_tag :li, link_to(title, path), class: opts[:class]
end
但是我找不到使用current_page的方法?来自NavList类。它包含了一个未找到的请求方法
答案 0 :(得分:0)
根据documentation当前页面?方法需要请求对象,也许您可以尝试将请求对象直接传递给链接方法。
def link(title, path, request, opts={})
css_class = 'inactive'
css_class = 'active' if current_page?(path)
content_tag :li, link_to(title, path), class: opts[:class]
end
<%= nl.link 'Employees', department_users_path(@department), request %>
答案 1 :(得分:0)
不确定是否有更好的方式
class NavList
attr_accessor :request
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
def header(title)
content_tag :li, title, class: 'nav-header'
end
def link(title, path, opts={class: ''})
opts[:class] = "#{opts[:class]} active" if current_page?(path)
content_tag :li, link_to(title, path), class: opts[:class]
end
end
def nav_list(&block)
new_block = Proc.new do
helper = NavList.new
helper.request = request
block.call(helper)
end
content_tag :ul, capture(&new_block), class: 'nav nav-list'
end