1

So I wrote this in my controller on a RoR app.

class KalendarController < ApplicationController

  def show
    @week =
    if Date.today.cweek % 2 == 0
      puts "E"
    else
      puts "O"
    end
  end
end

the @week variable should hold "E" or "N" depending on the result of Date.today.cweek % 2 == 0 so that I can display the letter "N" or "P" in the View

which looks something like this:

<h1>Kalendar#show</h1>
<p>Find me in app/views/kalendar/show.html.erb</p>


<p><%= @week %>

But this is not working. Can someone please point out what I'm doing wrong?

2 Answers 2

2

puts prints the string out the command line, but returns nil. So both branches of your if statement were returning return nil, and @week was set to nil.

So remove the puts and your good.

@week =
  if Date.today.cweek % 2 == 0
    "E"
  else
    "O"
  end
Sign up to request clarification or add additional context in comments.

1 Comment

you could do it in a single line as well with @week = Date.today.cweek % 2 == 0 ? "E" : "O"
1

You don't need modulus--you can use the even? method.

@week = Date.today.cweek.even? ? "E" : "O"

2 Comments

@week = Date.today.cweek.even? ? "E" : "O"
Had a typo, sorry. Try it now.

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.