rails_navigation
Simplify matching of Rails routes. You are unhappy with the link_to_if
helper?
Then check out this gem! You can now match controllers and actions as they are defined in your routes.rb.
Installation
Add this line to your application's Gemfile:
gem 'rails_navigation'
And then execute:
$ bundle
Usage
matches_page?
Works like current_page? Example:
matches_page?('controller#action')
with multiple conditions:
matches_page?(['controller#action', 'other_controller#action'])
nav_to
Uses matches_page?
and creates a link and a li element
Source code, if you want to customize it:
def nav_to(name, path, conditions = nil)
content_tag :li, link_to(name, path), class: ('active' if matches_page?(conditions))
end
Examples
You have a webseite full of books and want to show an edit button on the books#show page.
matches_page?('books#show')
It's like your routes.rb
Matching controllers:
{ controller: "pages" } becomes 'pages'
Matching controllers and actions:
{ controller: "pages", action: "new" } becomes 'pages#new'
Matching controllers, actions and params:
{ controller: "pages", action: "show", params: { id: 'homepage' } } becomes 'pages#new?id=homepage'
Or even simpler (id is the default):
{ controller: "pages", action: "show", params: { id: 'homepage' } } becomes 'pages#new?homepage'
What it does:
rails_navigation adds 2 helpers
- matches_page?(conditions)
- nav_to(name, path, conditions)
TODO
- support
not
operator - html options for inner link element
Maintainers
- Sascha Brink (https://github.com/sbrink)
- Martin Schurig (https://github.com/schurig)
Contributing
- Fork it ( https://github.com/sbrink/rails_navigation/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request