Gives you a couple of simple helpers which check the current request in a Rails application. It’s sort of a current_page? on steroids
gem install current
Let’s say you have a PostsController with a new action and you are currently on that page
controller_is('posts') # => true
This method also accepts multiple arguments and will return true if any of them match
controller_is('posts','users') # => true controller_is('sessions','users') # => false
The same logic applies to actions
action_is('new') # => true action_is('new','edit') # => true action_is('edit','index') # => false
If you want to be specific:
controller_action_is('posts','new') # => true controller_action_is('posts','index') # => false
Along with these methods you get a navigational link_to helper:
nav_link_to("Posts", posts_path, controller_is('posts'), options={}) # => "<a href="/posts" class="active"></a>" nav_link_to("Users", users_path, controller_is('users'), options={}) # => "<a href="/posts" class="inactive"></a>"
Notice that the third argument is a condition which we conveniently slap our helper into This will add a class of “active” or “inactive” to your link depending on whether that condition is true.
Note: An alternative for styling would be to include the controller and action names in your body id/class and use plain old CSS inheritance.
In your application layout
%body{:id => controller_name, :class => action_name} %ul#navigation %li= link_to "Posts", posts_path %li= link_to "Users", users_path
In your stylesheet
ul#navigation li a { background:white } body#posts ul#navigation li a, body#users ul#navigation li a { background:red }
I prefer to explicitly use the “active” classes for cleaner CSS, but whatever floats your boat.
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-
Send me a pull request. Bonus points for topic branches.
Copyright © 2010 Steven Garcia. See LICENSE for details.