Rails选项卡选项

mbr*_*ove 1 html ruby-on-rails

我有一个类似于标签的按钮界面,可以使用以下HTML与我的网站进行交互

<li class="current_page_item"><a href="/home" class="first">Home</a></li>
<li><a href="/projects">Projects</a></li>
<li><a href="/resume">Resume</a></li>
<li><a href="/contact">Contact</a></li>
Run Code Online (Sandbox Code Playgroud)

class="current_page_item属性更改选项卡的外观.每个网址都是一个托管我的StaticPages控制器的视图,如何让每个视图都选择了各自的"标签"?

Sim*_*tsa 5

current_page?(url) 方法可以帮到你.

创建一个额外的帮助方法.

module ApplicationHelper
  #...
  def tab_item(name, url)
    opts = {}
    opts[:class] = 'current_page_item' if current_page?(url)
    content_tag :li, opts do
      link_to name, url
    end
  end
  #...
end
Run Code Online (Sandbox Code Playgroud)

并在您的视图中使用它

<%= tab_item 'Home', root_path %>
<%= tab_item 'Projects', projects_path %>
<%= tab_item 'Resume', resume_path %>
<%= tab_item 'Contact', contact_path %>
Run Code Online (Sandbox Code Playgroud)