[英]Add an 'active' class to all active links in rails?
基本上,我有很多代碼看起來像這樣:
link_to t('.profile'), business_path(@business), class: '#{'active' if current_page? business_path(@business)}'
這不是很干。
我想知道是否有人知道修改 link_to 助手本身以自動向當前頁面的所有鏈接添加“活動”class 的好方法。
如果有幫助,我願意使用 HAML 或 SLIM。
我使用內置視圖助手current_page?
編寫了簡單的助手方法current_page?
當您可以在html_options
哈希中指定自定義class
名時。
def active_link_to(name = nil, options = nil, html_options = nil, &block)
active_class = html_options[:active] || "active"
html_options.delete(:active)
html_options[:class] = "#{html_options[:class]} #{active_class}" if current_page?(options)
link_to(name, options, html_options, &block)
end
示例(當您在root_path
路由上時):
<%= active_link_to "Main", root_path %>
# <a href="/" class="active">Main</a>
<%= active_link_to "Main", root_path, class: "bordered" %>
# <a href="/" class="bordered active">Main</a>
<%= active_link_to "Main", root_path, class: "bordered", active: "disabled" %>
# <a href="/" class="bordered disabled">Main</a>
這是一個已解決的問題,只需使用active_link_to gem。 您的示例簡化為:
= active_link_to t('.profile'), business_path(@business)
我遇到了同樣的要求,這是我的解決方案。
在ApplicationHelper
創建一個方法
def active_class(link_path)
current_page?(link_path) ? "active" : ""
end
在你的觀點中:
<li class="<%= active_class('/') %>">
<%= link_to 'HOME', root_path %>
</li>
這是編寫自己的包裝 link_to 幫助程序的好例子。 在您的 application_helper.rb 中,您可以編寫一個方法active_link_to
,它采用與 link_to + current_page 相同的參數,然后像上面一樣調用 link_to。
這是我使用的助手。 我添加了一個可選的“match_text”參數以增加靈活性(例如,如果我想在實際請求路徑是鏈接目標的子頁面時將鏈接標記為活動的。)
def link_to_active(text, destination, options = {})
match_text = options.delete(:match_text)
classes = options[:class].present? ? options[:class].split(" ") : []
classes << "active" if request.fullpath.downcase == destination.downcase || (match_text && request.fullpath.downcase.include?(match_text.downcase))
options = options.except(:class)
options.merge!(:class => classes.join(" ")) unless classes.empty?
link_to(text, destination, options)
end
如果您使用的是 rails 6.1+,這是一個很好的解決方案。
通過一個塊時也很有效。
def active_link_to(text = nil, path = nil, **opts, &block)
link = block_given? ? text : path
opts[:class] = class_names(opts[:class], { active: current_page?(link) })
if block_given?
link_to link, opts, &block
else
link_to text, path, opts
end
end
我和@egyamado 做了同樣的事情。 我也需要使用 AwesomeIcons,所以:
一個幫手:
def active_class?(link_path)
'active' if current_page?(link_path)
end
這是我的觀點:
<%= link_to my_controller_page_path,
:title => "My Controller Page",
:class => "other_name_class #{active_class?(my_controller_page_path)}" do %>
<i class="fa fa-fighter-jet"></i> My Controller Page
<%end%>
在另一種Link中,例如在一個Li里面。
#In this case I put a extra validation in root_path
<li class="nav-class <%=active_class?(my_controller_page_path)%> <%='active' if current_page?(root_path) %>">
<%= link_to my_controller_page_path,
:title => "Page 1",
:class => "other_name_class" do %>
Page 1
<%end%>
</li>
<li class="nav-class <%=active_class?(my_controller_page_2_path)%>">
<%= link_to my_controller_page_2_path,
:title => "Page 2",
:class => "other_name_class" do %>
Page 2
<%end%>
</li>
它對我有用。
根據 rails 6.1 現在我們有 html 類名的幫助程序
助手示例
class_names("foo", "bar")
# => "foo bar"
class_names({ foo: true, bar: false })
# => "foo"
class_names(nil, false, 123, "", "foo", { bar: true })
# => "123 foo bar"
你可以像這樣使用它
<%= link_to 'Home', root_path, class: class_names('nav-link', { active: current_page?(root_path) }) %>
它會產生這樣的 html
<a class="nav-link active" href="/">Home</a>
文檔在這里
這是我處理這個問題的自定義方法。
def active_link_to(name = nil, options = nil, html_options = nil, &block)
if current_page?(options)
active_class = html_options[:active_class] ? html_options[:active_class] : 'has-text-danger'
html_options[:class] << "#{html_options[:class]} #{active_class}"
end
link_to(name, options, html_options, &block)
end
html_options[:active_class]
是自定義哈希。
現在我可以動態更改活動鏈接的樣式。
<%= active_link_to "Menu", root_path, class: 'has-text-dark', active_class: 'has-text-danger' %>
這是您可以擴展的基本示例:
module ApplicationHelper
def active_link_to(text, path, **opts, &block)
css_class = opts[:class]
case opts[:endpoint]
when String then css_class.concat(' active') if current_page? opts[:endpoint]
when Array then css_class.concat(' active') if opts[:endpoint].include? request.path
end
if block_given?
link_to path, class: css_class, &block
else
link_to text, path, class: css_class
end
end
end
# app/views/layout/_navbar.html.erb
# Using multiple endpoints
<%= active_link_to 'Home', root_path, class: 'some-class', endpoint: ['', '/', '/home'] %>
# Using a single endpoint
<%= active_link_to 'Admin', admin_path, class: 'some-class', endpoint: '/admin' %>
# Using a block
<%= active_link_to admin_path, class: 'some-class', endpoint: '/admin' do %>
<h1>Administration</h1>
<% end %>
導航.苗條:
li class="nav-item"
= active_link_to 'Users', users_path, "nav-link"
application_helper.rb:
def active_link_to(title, path, class_css, options = {})
if current_page?(path)
class_css = "#{class_css} active"
options.merge!('aria-current': "page")
end
link_to title, path, class: class_css, **options
end
使用link_to_unless_current
然后讓它看起來像 CSS 中的活動鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.