Search code examples
rubyroutesreturnsinatra

Sinatra Route returns nothing


I have a very simple example where sinatra simply returns no output. The program enters the if clause but the block is not finished and therefore nothing is sent to rack, nothing goes to the browser... not a single character.

require 'sinatra'

 get '/' do
 var='confirmed'

   if var == 'confirmed'
     'Confirmed'
    end

  if var == 'declined'
    'Declined'
   end
 end

The question is now: Is adding a "return" or "next" the way this is usually done? With it, its running... But I never found an example in the net that had to use a next statement...
So, is the "if logic" usually somewhere else and there is only a single erb :xyz at the end of a route? I am confused...


Solution

  • It's nothing to do with the way Sinatra works, really. It's more of a Ruby matter. According to Sinatra readme:

    The return value of a route block determines at least the response body passed on to the HTTP client, or at least the next middleware in the Rack stack. Most commonly, this is a string, as in the above examples. But other values are also accepted.

    The problem in your code is that your last if is a statement itself. If your var variable isn't "declined", then the if block evaluates to nil, and as it is the last value in your route block, this is what gets returned by Sinatra.

    When you use explicit return, you don't get to the second if and don't have this issue, which is why it works with explicit return.


    You would not need an explicit return with a if/elsif block like this:

      # This is one single statement that would return either confirmed or declined
      # Request will always return a non-nil value
      get '/' do
        ...
        if var == 'confirmed'
          'Confirmed'
        elsif var == 'declined'
          'Declined'
        end
      end
    

    Or a case/when block:

      # This is one single statement that would return either confirmed or declined
      # Request will always return a non-nil value  
      get '/' do
        ...
        case var
        when 'confirmed' then 'Confirmed'
        when 'declined' then 'Declined'
        end
      end