# Mark html elements with a CSS class

**URL:** <https://www.ruby-forum.com/t/mark-html-elements-with-a-css-class/74056>\
**Category:** Rails\
**Created:** [October 29, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/mark-html-elements-with-a-css-class/74056 "2006-10-29T16:35:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gesen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f1d935/32.png) [@Gesen](https://www.ruby-forum.com/u/Gesen)\
**Post date:** [October 29, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/mark-html-elements-with-a-css-class/74056/1 "2006-10-29T16:35:10Z")

</div>

I’ve got a tab menu with links to different controllers. Depending on  
which controllers is the current, I would like the current controller’s  
tab to have a css class, e.g. class=“current”. I use the same layout  
file for all controllers.

I would like to do kind of the same thing to another menu but this time  
depending on which ID is the current. It’s a list of posts in the  
database, and depending on which post is the current, the current post  
shall have a css class in the list of posts.

Does anyone have a simple solutions for this?

---

<div class="post-metadata">

**Author:** ![Gesen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f1d935/32.png) [@Gesen](https://www.ruby-forum.com/u/Gesen)\
**Post date:** [October 29, 2006, 4:55pm UTC](https://www.ruby-forum.com/t/mark-html-elements-with-a-css-class/74056/2 "2006-10-29T16:55:13Z")

</div>

hi  
in body tag, put a class

then in your menu

- articles
- controller 
...

to format this, and highlight the current chosen controller, it would be  
in  
css file

.articles li .articles { highlighted style }

same for the highlighted posts

On 10/29/06, Gesen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> shall have a css class in the list of posts.
> 
> Does anyone have a simple solutions for this?
> 
> >

–  
Heri R.  
[http://sprinj.com](http://sprinj.com)

---

<div class="post-metadata">

**Author:** ![Gesen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f1d935/32.png) [@Gesen](https://www.ruby-forum.com/u/Gesen)\
**Post date:** [October 29, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/mark-html-elements-with-a-css-class/74056/3 "2006-10-29T19:05:29Z")

</div>

On 10/29/06, Heri R\> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> to format this, and highlight the current chosen controller, it would be  
> in css file
> 
> .articles li .articles { highlighted style }
> 
> same for the highlighted posts

Nice solution, but not very flexible. For such things I developed a  
plugin  
(site\_map) which is hosted on rubyforge. You can install it with:

./script/plugin install  
svn://rubyforge.org/var/svn/site-map/trunk/site\_map

The basics are:

1. in routes.rb define your site map:

SiteMap.draw do |map|

map.area 'public do  
map.section ‘about\_us’, :controller =\> ‘frontend’, :action =\>  
‘about\_us’  
map.section ‘signup’, :controller =\> ‘account’  
map.location :controller =\> ‘frontend’ # not bound to any  
particular  
section  
end

map.area ‘admin’ do  
map.section ‘users’, :controller =\> ‘/admin/users’  
map.section ‘posts’ do  
map.location :controller =\> ‘/admin/posts’  
map.location :controller =\> ‘/admin/comments’  
end  
end

end

1. Then in the views there is a current\_location helper which returns an  
object that describes current location. It has all the location  
attributes  
(area name, section name, etc) initialized based on current request  
params  
(including controller and action names)

You can use any attributes you want (zone / area / section / etc), there  
is  
no predefined ones (except you can’t use “location” attribute, because  
it is  
reserved keyword).

In your views you can use some helpers like  
\<%= section\_link ‘users’, ‘User management’, :controller =\>  
‘/admin/users’  
%\>

which is defined in helpers as:

def section\_link(section\_name, title, url\_options)  
if current\_location.section == section\_name  
# selected section  
link\_to title, url\_options, :class =\> ‘selected’  
else  
# normal section  
link\_to title, url\_options  
end  
end

This saves me from adding new stylesheets if I add new subdivisions to  
my  
web applications. It also gives greater flexibility in mapping  
controllers  
AND actions to different “locations”. Also, you can render selected e.g.  
“section” with simple text (not a link). With css solution you can’t do  
that.

---

<div class="post-metadata">

**Author:** ![Gesen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f1d935/32.png) [@Gesen](https://www.ruby-forum.com/u/Gesen)\
**Post date:** [November 7, 2006, 10:53am UTC](https://www.ruby-forum.com/t/mark-html-elements-with-a-css-class/74056/4 "2006-11-07T10:53:21Z")

</div>

I was thinking a slightly more easier solution. I solved the problem  
with the following solution.

\<% for page in @pages %\> class="current"\<% end %\>\>\<%= link\_to page.name, :controller =\> "pages", :action =\> "show", :id =\> page %\> \<% end %\>
