2011-02-24 106 views
1

我有翼狀按鍵接口與我的網站使用以下HTMLRails的標籤選擇

<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> 

class="current_page_item屬性更改標籤的外觀進行交互。每個網址都是我的StaticPages控制器管理的視圖,我怎樣才能讓每個視圖都有其各自的「選項卡」選擇?

回答

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 

而且用它在你看來

<%= tab_item 'Home', root_path %> 
<%= tab_item 'Projects', projects_path %> 
<%= tab_item 'Resume', resume_path %> 
<%= tab_item 'Contact', contact_path %> 
+1

這僅適用於僅希望在每個部分的根頁面上標籤頁的情況。如果您在「項目」中有任何子頁面,則它不會與當前頁面匹配,也不會「激活」該選項卡。 – bensie 2011-02-24 03:47:37

1

在你的控制器的方法添加before_filterset_current_tab

def set_current_tab 
    @current_tab = "Projects" 
end 

然後,您可以檢查此變種在你的意見和顯示選項卡,如果@current_tab等於當前選項卡行項目。

+0

如果在同一個控制器不同的標籤,你可以直接調用該方法從每個必要的控制方法,你可以添加接受當前的參數選項卡作爲字符串。 – bensie 2011-02-24 03:27:30

+0

您不應在視圖中設置實例變量。該視圖可以檢查ivars的值並作出相應的響應,但設置和方法調用屬於控制器。 – bensie 2011-02-24 03:39:17