Rails - текущая страница?

Варианты определения текущей страницы в Rails. Абсолютно стандартная задача в любом веб-приложении. Есть разные варианты определения текущей страницы, по факту текущего URL. Чаще всего используется изменения стилей элементов навигации или подключения разных ресурсов на разные страницы сайта.

Использовать params[:id]

= link_to 'My', my_path, class: ['navbar-item', ('is-active' if params[:id] == 'my')]

Использовать params[:action]

= link_to 'Order', form_path, class: ['navbar-item', ('is-active' if params[:action] == 'order')]

Использовать current_page?()

= link_to 'My', my_path, class: ['navbar-item', ('is-active' if current_page?(my_path))]

и ещё

current_page?('http://www.example.com/shop/checkout')
current_page?(product_path(@product))
current_page?(action: 'checkout')
current_page?('/shop/checkout')
current_page?(controller: 'shop', action: 'checkout', order: 'desc', page: '2')

Использовать current_controller?()

= link_to 'Articles', articles_path, class: ['navbar-item', ('is-active' if current_controller?(['articles']))]

Создать helper

module ApplicationHelper 
  def current_controller?(names)
    names.include?(controller_name)
  end
end

и использовать его

= current_controller?(['articles', 'news'])

Использовать request

request.path == '/'
request.url == '/'

Использовать link_to_if, link_to_unless

ul#navbar
  li= link_to_unless_current("Home", { action: "index" })
  li= link_to_unless_current("About Us", { action: "about" })