0

I need to change the color of my div button when the the user is logged in.

enter image description here

Once the user is logged in i want him seeing the "Download button" in red, like the "play now" button in the img.

This is the code now:

      #play_buttons
      #instant_play.play_button
        %p
          = "#{t :"asiaplus.download_client"}"
        .button
          = "#{t :"asianplus.download_now"}" 
      #play_now.play_button
        %p
          %span.highlight
            = "#{t :"asiaplus.web_based"}"
          = "#{t :"asiaplus.live_dealer"}"
        %a{:href => "#{live_casino_ho_lobby_asia_path(:tab => current_tab, :locale => current_locale)}", :class => "button", :title => "#{t :'asiaplus.play_now'}", :id => "ho_roulette"}
          = "#{t :"asiaplus.play_now"}"

.button is the class for the button "Download now" on the left and :id => "ho_roulette" gives the style for the "Play now" button on the right.

Ok this is my solution to achieve the Download now button style like the Play Now button when the user is logged in:

       #play_buttons
     - if logged_in 
      #instant_play.play_button
        %p
          = "#{t :"asiaplus.download_client"}"
        .button
          = "#{t :"asianplus.download_now" :id => "ho_roulette"}" 
      #play_now.play_button
        %p
          %span.highlight
            = "#{t :"asiaplus.web_based"}"
          = "#{t :"asiaplus.live_dealer"}"
        %a{:href => "#{live_casino_ho_lobby_asia_path(:tab => current_tab, :locale => current_locale)}", :class => "button", :title => "#{t :'asiaplus.play_now'}", :id => "ho_roulette"}
          = "#{t :"asiaplus.play_now"}"
    - else 
      #instant_play.play_button
        %p
          = "#{t :"asiaplus.download_client"}"
        .button
          = "#{t :"asianplus.download_now"}" 
      #play_now.play_button
        %p
          %span.highlight
            = "#{t :"asiaplus.web_based"}"
          = "#{t :"asiaplus.live_dealer"}"
        %a{:href => "#{live_casino_ho_lobby_asia_path(:tab => current_tab, :locale => current_locale)}", :class => "button", :title => "#{t :'asiaplus.play_now'}", :id => "ho_roulette"}
          = "#{t :"asiaplus.play_now"}"

Basically i have used conditional statements adding :id => "ho_roulette" to my div button when the user is logged in.

Is this solution correct? i thought also i can use the status class active as other solution, but i did not want to touch my SASS file.

1
  • = "#{t()}" is the bad form for: = t() Commented Feb 24, 2013 at 19:02

2 Answers 2

3

There are two things I would revise about your solution:

  1. HTML id tags should be unique, so you shouldn't have two different elements with the same ID. You definitely want to use class instead.

  2. You're repeating a whole lot of code here. It would be much better to use the conditional inside of your tag, and take advantage of the fact that HAML ignores keys with nil values:

    play_buttons
      #instant_play.play_button
      %p
        = t :"asiaplus.download_client"
      .button
        = t :"asianplus.download_now", :class => ("active" if logged_in)
      #play_now.play_button
      %p
        %span.highlight
          = t :"asiaplus.web_based"
        = t :"asiaplus.live_dealer"
      = link_to t("asiaplus.play_now"), live_casino_ho_lobby_asia_path(:tab => current_tab, :locale => current_locale), :class => ["button", ("active" if logged_in)], :title => t('asiaplus.play_now'), :id => "ho_roulette"}
    
Sign up to request clarification or add additional context in comments.

3 Comments

Even though I agree 100% with you, he said he didn't want to touch his css file.
i have not understoood why you repeat :id => "ho_roulette" at the end if you have already add :class => ["button", ("active" if logged_in)]
I added that because I'm assuming that he's using that id tag as an actual id for an element (e.g. for more styling or to trigger some Javascript). If it's only being used for this particular styling effect then it can go away.
2

Your solution is good. Conditional CSS is the easiest solution. You should dry it up though. Instead of having the whole thing in the if/else statement, simply do it for your id:

  #instant_play.play_button
    %p
      = t(:"asiaplus.download_client")
    .button
      = t(:"asianplus.download_now"), :id => ('ho_roulette' if logged_in)
  #play_now.play_button
    %p
      %span.highlight
        = t(:"asiaplus.web_based")
      = t(:"asiaplus.live_dealer")
    %a{:href => live_casino_ho_lobby_asia_path(:tab => current_tab, :locale => current_locale), :class => "button", :title => t(:'asiaplus.play_now'), :id => "ho_roulette"}
      = t(:"asiaplus.play_now")

To consider:

  • You should follow @apneadiving's advice to reformat your t().

  • @charleyc is 100% right with regards to having id unique. Consider using his solution and changing your SASS file... and your approach to your use of id tags to define CSS rules. States (like 'active' or 'ho_roulette') should always be CSS classes.

2 Comments

What is best to use inside the bracket : "" or ''? you have used '' but @charleyc use "" For example class => ('active', if logged_in) or class => ("active" if logged_in)
It is entirely up to you. I prefer to use single quotes everywhere I can and only use double when doing string interpolation. In most cases, it doesn't matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.