Ruby on rails RubyonRails和选项卡

Ruby on rails RubyonRails和选项卡,ruby-on-rails,tabs,Ruby On Rails,Tabs,我还是ruby和rails的初学者,现在我在谷歌上搜索创建选项卡式菜单的方法,用css类“current”标记当前活动控制器的列表元素。谷歌上有很多点击率,但我还没有找到任何我能成功工作的 我这里有我的菜单: <ul> <li class="current"><%= link_to 'Home', root_path %> </li> <li><%= link_to 'Browse songs', page_path('b

我还是ruby和rails的初学者,现在我在谷歌上搜索创建选项卡式菜单的方法,用css类“current”标记当前活动控制器的列表元素。谷歌上有很多点击率,但我还没有找到任何我能成功工作的

我这里有我的菜单:

<ul>
  <li class="current"><%= link_to 'Home', root_path %> </li>
  <li><%= link_to 'Browse songs', page_path('browse') %> </li>
  <li><%= link_to 'Add song', new_song_path %> </li>
  <li><%= link_to 'Request song', artists_path %> </li>
  <li><%= link_to 'My ReChord', artists_path %> </li>
  <li><%= link_to 'Help', page_path('help') %> </li>
  <li id="search"><form><input type="search" placeholder="Type here to find a song or an artist"/></form> </li>
  <li class="notab">
    <% if user_signed_in? %>
      <%= link_to 'Sign out', destroy_user_session_path %>
    <% else %>
      <%= link_to 'Sign in', new_user_session_path %> or
      <%= link_to 'sign up', new_user_registration_path %>
    <% end %>
  </li>
</ul>
现在我在主页选项卡上硬编码了class=“current”。但是,当单击例如浏览歌曲时,我希望class=“current”移动到该行对应的列表元素

请注意,我有一些只是路由路径的链接(如new_song_path),还有一些是子页面的链接,如page_path(“help”)。我需要它来为这两种类型的链接工作

你能为我提供一个适合我两天的rails经验的好教程,或者(最好)提供一个可能完全适合我上面列表的示例代码吗?提前谢谢

为您创建该菜单的帮助器:

def menu_builder(page_id)
  tabs = ['home','store','faq']
  content = ""
  tabs.each do |tab|
    content << if page_id == tab
      content_tag('li', content_tag('a', tab, :href => nil ), :class => 'current') + " "
    else
      content_tag('li', content_tag('a', tab, :href => "/#{tab}" )) + " "
    end
  end
  content
end
然后在模板中使用它:

<ul>
    <%= menu_builder(@page_id) %>
    <li class="search">...</li>
</ul>

如果您想要更灵活的东西,请查看我创建的插件,它正好解决了这个常见模式

在模板中,使用
tabs\u标记
helper创建选项卡

<% tabs_tag do |tab| %>
  <%= tab.home      'Homepage', root_path %>
  <%= tab.dashboard 'Dashboard', dashboard_path %>
  <%= tab.account   'Account', account_path %>
<% end %>
现在,如果操作属于
DashboardController
,模板将自动呈现以下HTML代码

<ul>
  <li><a href="/">Homepage</a></li>
  <li class="custom"><span>Dashboard</span></li>
  <li><a href="/account">Account</a></li>
</ul>
  • 仪表板

看看这个插件:

它有很多很酷的组件,包括导航

文档:

我创建文档就是为了解决这个问题。有一个单独的选项卡配置文件,您可以在其中描述希望选项卡的行为。例如:

Tabulous.setup do

  tabs do
    pictures_tab do
      text          { 'Pictures' }
      link_path     { pictures_path }
      visible_when  { true }
      enabled_when  { true }
      active_when   { in_action('any').of_controller('pictures') }
    end

    music_tab do
      text          { 'Music' }
      link_path     { songs_path }
      visible_when  { true }
      enabled_when  { current_user.has_music? }
      active_when   { in_action('any').of_controller('songs') }
    end

    profile_tab do
      text          { 'My Profile' }
      link_path     { account_path(current_user) }
      visible_when  { true }
      enabled_when  { true }
      active_when   { in_actions('show', 'edit', 'update').of_controller('accounts') }
    end
  end

end

阅读了解更多信息。

小部件选项卡导航的忠实粉丝——已经在许多项目中使用过它谢谢!但是我如何才能使它与page_path(“浏览”)和page_path(“帮助”)一起工作,这是两个不同的选项卡?我的意思是,我有一个名为page的控制器。在控制器中,我有一个名为show的函数。在该函数中,我确实呈现:partial=>params[:id]。我需要以某种方式使用动态参数运行set_选项卡。我对rails完全陌生。你可以在你的操作中调用set_tab或使用before_过滤器。好的,但我的意思是,我如何使用set_tab和动态值?我的意思是,我只有一个带有render的操作:partial=>params[:id]。我想要像set_tab params[:id],但这似乎不起作用。是的,您可以这样做。但是,在模板中,您需要配置一个选项卡。对于每个可能的ID,您是否阅读了文档?我想为Tabulous添加一个建议。我使用它已经有一段时间了,它是一个非常好的工具。我必须将.html\u safe添加到从菜单返回构建器(page\u id)方法中才能让它工作
<ul>
  <li><a href="/">Homepage</a></li>
  <li><a href="/dashboard">Dashboard</a></li>
  <li><a href="/account">Account</a></li>
</ul>
class DashboardController < ApplicationController
  set_tab :dashboard
end
<ul>
  <li><a href="/">Homepage</a></li>
  <li class="custom"><span>Dashboard</span></li>
  <li><a href="/account">Account</a></li>
</ul>
Tabulous.setup do

  tabs do
    pictures_tab do
      text          { 'Pictures' }
      link_path     { pictures_path }
      visible_when  { true }
      enabled_when  { true }
      active_when   { in_action('any').of_controller('pictures') }
    end

    music_tab do
      text          { 'Music' }
      link_path     { songs_path }
      visible_when  { true }
      enabled_when  { current_user.has_music? }
      active_when   { in_action('any').of_controller('songs') }
    end

    profile_tab do
      text          { 'My Profile' }
      link_path     { account_path(current_user) }
      visible_when  { true }
      enabled_when  { true }
      active_when   { in_actions('show', 'edit', 'update').of_controller('accounts') }
    end
  end

end