Ad

Ruby Check If It's October To Display Pink Logo

- 1 answer

I'd like to use a different logo for breast cancer awareness month (October)...I'd also like the logo to be tested using params so I can ensure it's working up to snuff before October. Clearly doing something wrong!

Controller:

def breast_cancer_logo_month
  if params[:breast_cancer_logo_month] || Time.current.month = 10
    return true
  end
  false
end

view:

<% if breast_cancer_logo_month  %>
  #breast cancer logo
<% else %>
  #standard logo
<% end %>
Ad

Answer

Your breast_cancer_logo_month is wrong. You are using a simple equal when you need two. One equal will try override Time.current.month value which will raise an error.
Plus if you return true or false in a condition, you can just return the condition. And you can add a ? to the method name too.

def breast_cancer_logo_month?
  !!params[:breast_cancer_logo_month] || Time.current.month == 10
end

<% if breast_cancer_logo_month? %>
  #breast cancer logo
<% else %>
  #standard logo
<% end %>
Ad
source: stackoverflow.com
Ad