css - Custom Navigation Bar Color by URL ID -
i've created traditional navigation bar. works great, customize navbar color district url.
for example:
###blue http://0.0.0.0:3000/districts/1 ###red http://0.0.0.0:3000/districts/2 ###orange http://0.0.0.0:3000/districts/3 etc.
i've hacked out solution, isn't efficient when add more districts. somehow loop through each district , associated css_color.
district params
:name, :css_color, :photo
css
.navbar1 { background-color: #4b8dc8; } .navbar2 { background-color: #406da6; } .navbar3 { background-color: #640000; } .navbar4 { background-color: #1450a3; } .navbar5 { background-color: #cbaa7f; }
_navigation.html.erb
###how can more efficiently <% if params[:controller] == 'static_pages' || params[:controller] == 'searches' %> <nav class="navbar navbar-default navbar-fixed-top"> <% elsif params[:controller] == 'sessions' %> <nav class="navbar navbar-default navbar-fixed-top"> <% elsif params[:controller] == 'password_resets' %> <nav class="navbar navbar-default navbar-fixed-top"> <% elsif params[:controller] == 'resource_tags' %> <nav class="navbar navbar-default navbar-fixed-top"> <% elsif params[:controller] == 'favorites' %> <nav class="navbar navbar-default navbar-fixed-top"> <% elsif @districts %> <nav class="navbar navbar-default navbar-fixed-top"> ###beg of districts <% elsif @district.id == 1 %> <nav class="navbar navbar1 navbar-default navbar-fixed-top"> <% elsif @district.id == 2 %> <nav class="navbar navbar2 navbar-default navbar-fixed-top"> <% elsif @district.id == 3 %> <nav class="navbar navbar3 navbar-default navbar-fixed-top"> <% elsif @district.id == 4 %> <nav class="navbar navbar4 navbar-default navbar-fixed-top"> <% elsif @district.id == 5 %> <nav class="navbar navbar5 navbar-default navbar-fixed-top"> <% end %>
firstly - looks want common navbar stuff options. difference difference if have districts. means don't need guff checking ever kind of controller or session. just:
<% if @district.present? %> ... you'll district-specific stuff here <% else %> <nav class="navbar navbar-default navbar-fixed-top"> <% end %>
right? no sense in repeating same code on , on multiple options, if it's there.
now need figure out put in if have district... id one-to-one match css style_names
navbar1 district id 1 navbar2 district id 2 etc...
so don't need more id:
<% if @district.present? %> <nav class="navbar navbar<%= @district.id %> navbar-default navbar-fixed-top"> <% else %> <nav class="navbar navbar-default navbar-fixed-top"> <% end %>
but there's still duplication here. thing differs between navbar @ top (in if-clause) , navbar @ bottom (in else-clause) style... can cut down if/else surround difference this:
<nav class="navbar <%= "navbar#{@district.id}" if @district.present? %> navbar-default navbar-fixed-top">
Comments
Post a Comment