2011-03-28 85 views
0

以下this snippet,我已經在我的rails應用程序中進行了設置,以突出顯示當前正在呈現的鏈接。遇到突出顯示當前鏈接呈現的問題

<div class="env-alt rtl"> 
    <%= section_link("Home", :controller => 'welcome') %> | 
    <% if user_signed_in? %> 
     <%= section_link("Map", :controller => 'pois') %> | 
     <%= section_link("News", :controller => 'news', :ref => 'home') %> | 
     <%= section_link("Documents", :controller => 'documents', :ref => 'home') %> | 
     <%= section_link("Organisations", :controller => 'organisations', :ref => 'home') %> | 
     <%= section_link("Dashboard", :controller => 'welcome', :action => 'dashboard') %> | 
    <%#= link_to "Sign out", destroy_user_session_path, :id => 'sign_out' %> 
    <% else %> 
    <%= link_to "Sign up", new_user_registration_path, :id => 'sign_up' %> | 
    <%= link_to "Sign in", new_user_session_path, :id => 'sign_in' %> 
    <% end %> 
</div> 

在application_controller.rb我有以下幾點:

def instantiate_controller_and_action_names 
    @current_action   = action_name 
    @current_controller = controller_name 
end 

在application_helper.rb:

def section_link(name, options) 
    action  = options[:action] || 'index' 
    controller = options[:controller] 

    if action.eql?(@current_action) and controller.eql?(@current_controller) 
     link_to(name, options, :class => 'youarehere') 
    else 
    link_to(name, options) 
    end 
end 

我覺得,我把一切都設置正確。但是,在我拋出這個奇怪的錯誤:

Showing /home/syed/work/projects/mapunity/environment/app/views/shared/links/_user.erb where line #2 raised: 

No route matches {:controller=>"devise/welcome"} 

Extracted source (around line #2): 

1: <div class="env-alt rtl"> 
2: <%= section_link("Home", :controller => 'welcome') %> | 
3: <% if user_signed_in? %> 
4: <%= section_link("Map", :controller => 'pois') %> | 
5: <%= section_link("News", :controller => 'news', :ref => 'home') %> | 

爲什麼會自動添加:controller => "devise/welcome"?任何指向我出錯的指針將不勝感激。提前致謝。

回答

0

回答我的問題,可能會有所幫助別人,必須更新我的section_link_to助手這樣的:

定義的CSS類.current突顯當前的鏈接。我的定義是這樣的:

.current { 
    font-size: 12pt; 
    text-decoration: none; 
} 

application_controller.rb,定義哪些將設置電流控制器和動作名稱的的before_filter。

def instantiate_controller_and_action_names 
    @current_action = action_name 
    @current_controller = controller_name 
end 

然後助手:

def section_link_to(name, url_options, html_options = {}) 
    if url_options.is_a?(Hash) 
    action = url_options[:action] || 'index'   
    controller = url_options[:controller] 

    if action.eql?(@current_action) and controller.eql?(@current_controller) 
     link_to(name, url_options, html_options, :class => 'current') 
    else 
     link_to(name, url_options, html_options) 
    end 
    else 
    if url_options.length > 1 
     controller = url_options.delete('/') 
     if controller.include?('?') 
     controller = controller.split('?')[0] 
     end 
    else 
     controller = 'welcome' 
    end 

    if controller == @current_controller 
     if html_options.has_key?(:class) 
     css_options = html_options.fetch(:class) 
     css_options << ' current' 

     html_options.merge!({ :class => css_options }) 
     else 
     html_options.merge!({ :class => 'current' }) 
     end 

     link_to(name, url_options, html_options) 
    else 
     link_to(name, url_options, html_options) 
    end 
    end 
end 

在幫手,我檢查current_controllercurrent_action是否等於所呈現的鏈接。如果是,請將類.current添加到該鏈接並進行渲染。

現在我可以通過或者是像哈希選項:

<%= section_link_to "Home", :controller => 'welcome %> 

或者使用產生的軌道是這樣的:

<%= section_link_to "Home", root_path %>