moneymakesadifference
moneymakesadifference

Reputation: 55

Using dynamic class in rails app

Rails project.
I have a sidenav that only gets generated on specific controllers. Now I need to offset content on pages where the sidenav is active.
Therefore I added in the application helper:

    def sidebarGenerated
    'class="sidebarActive"' if controller_name == "controller1" || 'controller2' || 'controller3'
end

and in the application view:

<body <%= sidebarGenerated %> >
  <header <%= sidebarGenerated %> ><%= render 'layouts/header' %></header>
  <main <%= sidebarGenerated %> >

and in application.scss:

    .sidebarActive {
  header, main, footer {
   padding-left: 240px;
 }

However, content doesn't offset on pages using the mentioned controllers. Any idea why? thanks

Upvotes: 0

Views: 29

Answers (2)

ShallmentMo
ShallmentMo

Reputation: 449

Your css must be wrong.I think what you want is something like this:

header.sidebarActive, main.sidebarActive, footer.sidebarActive {
   padding-left: 240px;
}

or like this:

header, main, footer{
  &.sidebarActive{
    padding-left: 240px;
  }
}

PS:@Brad Werth is right, there is some problem in your condition.

Upvotes: 0

Brad Werth
Brad Werth

Reputation: 17647

There is a flaw in your logic at:

if controller_name == "controller1" || 'controller2' || 'controller3'

What you really want is:

if controller_name == "controller1" || controller_name == 'controller2' || controller_name == 'controller3'

Which can be better expressed by:

if %w[ controller1 controller2 controller3 ].include?( controller_name )

Upvotes: 1

Related Questions