0

我正在尝试构建一个块助手,但我似乎无法找到访问 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 类中。它与未找到的请求方法有关

4

2 回答 2

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
于 2013-09-19T23:20:04.727 回答
0

根据文档current_page? 方法需要请求对象,也许您可​​以尝试将请求对象直接传递给链接方法。

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 %>
于 2013-09-19T22:41:11.653 回答