1

I am trying to write something like this, but it display me the whole string. Not the formatted "Today!". Feel free to tune my method :) thanks

def days_left(deadline)
  (if deadline.date-Date.today == 0
    "<strong>Today!</strong>"
  elsif deadline.date-Date.today < 1
    "<div class='expired'>Overdue</div>"
  else
    (deadline.date-Date.today).to_i
  end)
end

1 Answer 1

4
def days_left(deadline)
  (if deadline.date-Date.today == 0
    "<strong>Today!</strong>"
  elsif deadline.date-Date.today < 1
    "<div class='expired'>Overdue</div>"
  end).html_safe
end

Or, display it in the view with

<%= raw days_left(d) %>
Sign up to request clarification or add additional context in comments.

3 Comments

it gives me 'undefined method `html_safe' for (138/1):Rational'
Replace .html_safe with .class and double-check that it's a String. Rails3 monkey-patches String and adds an html_safe attribute. You could also just try .to_s.html_safe because it appears your actual code is returning a Ruby Standard Library Rational class number and not a String.
I should note that the parens around the if..end are entirely optional, it's OK to just say end.html_safe if you like that better.

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.