Syntax Error: unexpected $ end when using if / else if?

I am getting an error from one of my controller classes, and I cannot understand why. Error:

SyntaxError in TermsController#show, syntax error, unexpected $end, expecting keyword_end

Here are the terms_controller.rb:

class TermsController < ApplicationController

    def show
        @term = Term.find(params[:id])
        if @term.id == 1
            @title = "Fall"
        else if @term.id == 2
            @title = "Winter"
        else if @term.id == 3
            @title = "Spring"
        else if @term.id == 4
            @title = "Summer"
        end 
    end 
end 

My show page consists of:

<h1> <%= @title %> </h1>

Maybe something small that I just missed - thanks for your help!

+5
source share
3 answers

The problem is that there are not enough keywords end, and I discovered $end(a token representing the end of the file) before he can find what he was looking for - another one end. (The parser token for the keyword endis either "keyword_end" or "Kend", depending on the version of ruby.)

if end.

, elsif else if. if end ( if end).

   if x == 1
     "1"
   elsif x == 2
     "2"
   else
     "else"        
   end

case, , (x ):

   case x
     when 1 then "1"
     when 2 then "2"
     else "else"
   end

else if (, if if), , , if. , .

   if x == 1
     "1"
   else
     if x == 2
       "2"
     else
       "else"
     end
   end

.


: if, expr if cond, end , , , .

, if case Ruby,

@term = Term.find(params[:id])
@title = case @term.id
   when 1 then "Fall"
   when 2 then "Winter"
   when 3 then "Spring"
   when 4 then "Summer"
   else "Invalid Term"
end 

if/elsif/end , case @term.id. - Hash - , ; -)

+10

else if elsif.

+1

:

class TermsController < ApplicationController

  @@seasons = { 1 => "Fall", 2 => "Winter", 3 => "Spring", 4 => "Summer"}

  def show
    @term = Term.find(params[:id])
    @title = @@seasons[params[:id]] || "Invalid Season"
  end

end
+1

All Articles