Ruby on rails 3 实现高亮显示当前渲染的链接时遇到问题
接下来,我在rails应用程序中设置了高亮显示当前呈现的链接Ruby on rails 3 实现高亮显示当前渲染的链接时遇到问题,ruby-on-rails-3,Ruby On Rails 3,接下来,我在rails应用程序中设置了高亮显示当前呈现的链接 <div class="env-alt rtl"> <%= section_link( "Home", :controller => 'welcome' ) %> | <% if user_signed_in? %> <%= section_link( "Map", :controller => 'pois' ) %> | <%= s
<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>
在应用程序_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' ) %> |
显示/home/syed/work/projects/mapunity/environment/app/views/shared/links/_user.erb,其中第2行出现:
没有路由匹配{:controller=>“设计/欢迎”}
提取的源(第2行附近):
1:
2:‘欢迎’)%%>|
三:
4:‘POI’”%%>|
5:‘新闻’,:ref=>‘主页’%%>|
为什么它会自动添加
:controller=>“设计/欢迎”
?如果有人能指出我错在哪里,我们将不胜感激。提前感谢。回答我自己的问题,可能会对其他人有所帮助,必须更新我的部分链接到帮助者,如下所示:
<%= section_link_to "Home", root_path %>
定义了一个css类.current
,以突出显示当前链接。我的定义如下:
.current {
font-size: 12pt;
text-decoration: none;
}
在应用程序\u controller.rb
中,定义一个before\u过滤器,用于设置当前控制器和操作名称
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
或者像这样使用生成的rails:
<%= section_link_to "Home", root_path %>
<%= section_link_to "Home", root_path %>