gmail
2006-Feb-16 13:08 UTC
[Rails] RE: Applying list-item format depending on action (Diego Scataglini)
Seth, It really depends how often you do that check. If it happens only once or twice in your whole application, that''s just fine. If it happens more than that and in multiple controllers/views then maybe you want to create a helper for it. Something like this: (didn''t test it, but should be working) module ApplicationHelper def current_id_select(controller, action) ( (params[:controller] == controller) and (params[:action] == action) ) ? '' id="current"'' : '''' end end Then in the view you just go <li <%= current_id_select(''admin'', ''list'') %>>List </li> <li <%= current_id_select(''admin'', ''index'') %>>Index</li> <li <%= current_id_select(''admin'', ''search'') %>>Search</li> If this is what you wanted. Btw, if this where links to pages, you could just extend the link_to to automatically add a class or id set to "current" if the link_to target and the params coincided. Or you could put the [list, index, search] in a hash like this myhash = {"list" => ["admin", "list"], "index" => ["admin", "index"]} and do something like this in the view <% myhash.each do |idx, pparams| -%> <%= "<li #{current_id_select(pparams[0], pparams[1])}>#{idx}</li>" %> <% end -%> I would do this if the list of links/li was dynamic or db driven. Then again if I was doing this very often I would probably create a component or helper like draw_left_nav_li or something like that that would encapsulate the whole thing. I guess it depends. :D Diego Scataglini
Seemingly Similar Threads
- Sorting a hash
- clang error: static_assert failed "Cache the hash code or make functors involved in hash code and bucket index computation default constructible"
- Applying list-item format depending on action
- http request, hash table
- How to store the same key multi times in a Hash ?