All Projects → comfy → Active_link_to

comfy / Active_link_to

Licence: mit
Rails view helper to manage "active" state of a link

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Active link to

Navigasmic
Navigasmic: Semantic navigation for Rails using simple view level or configuration definitions.
Stars: ✭ 327 (-58.71%)
Mutual labels:  rails, navigation
Simple Navigation
A ruby gem for creating navigations (with multiple levels) for your Rails, Sinatra or Padrino applications. Render your navigation as html list, link list or breadcrumbs.
Stars: ✭ 868 (+9.6%)
Mutual labels:  rails, navigation
Jetpack Mvvm Best Practice
是 难得一见 的 Jetpack MVVM 最佳实践!在 以简驭繁 的代码中,对 视图控制器 乃至 标准化开发模式 形成正确、深入的理解!
Stars: ✭ 6,950 (+777.53%)
Mutual labels:  navigation
Rails
Official Ruby on Rails specific tasks for Capistrano
Stars: ✭ 773 (-2.4%)
Mutual labels:  rails
Rails Handbook
Describing the development process used by the Infinum Rails Team.
Stars: ✭ 738 (-6.82%)
Mutual labels:  rails
Ypnavigationbartransition
A Full functional UINavigationBar framework for making bar transition more natural! You don't need to call any UINavigationBar api, implementing YPNavigationBarConfigureStyle protocol for your view controller instead. (类似微信 iOS Navigation Bar 的切换方案)
Stars: ✭ 725 (-8.46%)
Mutual labels:  navigation
Activerecord Postgis Adapter
ActiveRecord connection adapter for PostGIS, based on postgresql and rgeo
Stars: ✭ 746 (-5.81%)
Mutual labels:  rails
Railsgoat
A vulnerable version of Rails that follows the OWASP Top 10
Stars: ✭ 699 (-11.74%)
Mutual labels:  rails
Materialize Sass
Materializecss rubygem for Rails Asset Pipeline / Sprockets
Stars: ✭ 785 (-0.88%)
Mutual labels:  rails
Rails Style Guide
A community-driven Ruby on Rails style guide
Stars: ✭ 6,178 (+680.05%)
Mutual labels:  rails
Rails Template
Application template for Rails 6 projects; preloaded with best practices for TDD, security, deployment, and developer productivity.
Stars: ✭ 763 (-3.66%)
Mutual labels:  rails
Brakeman
A static analysis security vulnerability scanner for Ruby on Rails applications
Stars: ✭ 6,281 (+693.06%)
Mutual labels:  rails
Access Granted
Multi-role and whitelist based authorization gem for Rails (and not only Rails!)
Stars: ✭ 733 (-7.45%)
Mutual labels:  rails
Buffalo
Rapid Web Development w/ Go
Stars: ✭ 6,476 (+717.68%)
Mutual labels:  rails
Action policy
Authorization framework for Ruby/Rails applications
Stars: ✭ 718 (-9.34%)
Mutual labels:  rails
React Rails
Integrate React.js with Rails views and controllers, the asset pipeline, or webpacker.
Stars: ✭ 6,417 (+710.23%)
Mutual labels:  rails
Redis Search
Deprecated! High performance real-time prefix search, indexes store in Redis for Rails application
Stars: ✭ 713 (-9.97%)
Mutual labels:  rails
Paper trail
Track changes to your rails models
Stars: ✭ 6,185 (+680.93%)
Mutual labels:  rails
Priority Navigation
Javascript implementation for Priority+ Navigation — no dependencies
Stars: ✭ 739 (-6.69%)
Mutual labels:  navigation
Graphiti
Stylish Graph APIs
Stars: ✭ 783 (-1.14%)
Mutual labels:  rails

active_link_to

Creates a link tag of the given name using a URL created by the set of options. Please see documentation for link_to, as active_link_to is basically a wrapper for it. This method accepts an optional :active parameter that dictates if the given link will have an extra css class attached that marks it as 'active'.

Gem Version Gem Downloads Build Status Gitter

Install

When installing for Rails 3/4/5 applications add this to the Gemfile: gem 'active_link_to' and run bundle install.

For older Rails apps add config.gem 'active_link_to' in config/environment.rb and run rake gems:install. Or just checkout this repo into /vendor/plugins directory.

Super Simple Example

Here's a link that will have a class attached if it happens to be rendered on page with path /users or any child of that page, like /users/123

active_link_to 'Users', '/users'
# => <a href="/users" class="active">Users</a>

This is exactly the same as:

active_link_to 'Users', '/users', active: :inclusive
# => <a href="/users" class="active">Users</a>

Active Options

Here's a list of available options that can be used as the :active value

* Boolean                         -> true | false
* Symbol                          -> :exclusive | :inclusive | :exact
* Regex                           -> /regex/
* Controller/Action Pair          -> [[:controller], [:action_a, :action_b]]
* Controller/Specific Action Pair -> [controller: :action_a, controller_b: :action_b]
* Hash                            -> { param_a: 1, param_b: 2 }

More Examples

Most of the functionality of active_link_to depends on the current url. Specifically, request.original_fullpath value. We covered the basic example already, so let's try something more fun.

We want to highlight a link that matches immediate url, but not the children nodes. Most commonly used for 'home' links.

# For URL: /users will be active
active_link_to 'Users', users_path, active: :exclusive
# => <a href="/users" class="active">Users</a>
# But for URL: /users/123 it will not be active
active_link_to 'Users', users_path, active: :exclusive
# => <a href="/users">Users</a>

If we need to set link to be active based on some regular expression, we can do that as well. Let's try to activate links urls of which begin with 'use':

active_link_to 'Users', users_path, active: /^\/use/

If we need to set link to be active based on an exact match, for example on filter made via a query string, we can do that as well:

active_link_to 'Users', users_path(role_eq: 'admin'), active: :exact

What if we need to mark link active for all URLs that match a particular controller, or action, or both? Or any number of those at the same time? Sure, why not:

# For matching multiple controllers and actions:
active_link_to 'User Edit', edit_user_path(@user), active: [['people', 'news'], ['show', 'edit']]

# For matching specific controllers and actions:
active_link_to 'User Edit', edit_user_path(@user), active: [people: :show, news: :edit]

# for matching all actions under given controllers:
active_link_to 'User Edit', edit_user_path(@user), active: [['people', 'news'], []]

# for matching all controllers for a particular action
active_link_to 'User Edit', edit_user_path(@user), active: [[], ['edit']]

Sometimes it should be as easy as giving link true or false value:

active_link_to 'Users', users_path, active: true

If we need to set link to be active based on params, we can do that as well:

active_link_to 'Admin users', users_path(role_eq: 'admin'), active: { role_eq: 'admin' }

More Options

You can specify active and inactive css classes for links:

active_link_to 'Users', users_path, class_active: 'enabled'
# => <a href="/users" class="enabled">Users</a>

active_link_to 'News', news_path, class_inactive: 'disabled'
# => <a href="/news" class="disabled">News</a>

Sometimes you want to replace link tag with a span if it's active:

active_link_to 'Users', users_path, active_disable: true
# => <span class="active">Users</span>

If you are constructing navigation menu it might be helpful to wrap links in another tag, like <li>:

active_link_to 'Users', users_path, wrap_tag: :li
# => <li class="active"><a href="/users">Users</a></li>

You can specify css classes for the wrap_tag:

active_link_to 'Users', users_path, wrap_tag: :li, wrap_class: 'nav-item'
# => <li class="nav-item active"><a href="/users">Users</a></li>

Helper Methods

You may directly use methods that active_link_to relies on.

is_active_link? will return true or false based on the URL and value of the :active parameter:

is_active_link?(users_path, :inclusive)
# => true

active_link_to_class will return the css class:

active_link_to_class(users_path, active: :inclusive)
# => 'active'

Copyright

Copyright (c) 2009-18 Oleg Khabarov. See LICENSE for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].