Sinatra 路线 returns 无
Sinatra Route returns nothing
我有一个非常简单的示例,其中 sinatra 只是 returns 没有输出。
程序进入 if 子句但块未完成,因此没有任何内容发送到机架,没有任何内容发送到浏览器...不是单个字符。
require 'sinatra'
get '/' do
var='confirmed'
if var == 'confirmed'
'Confirmed'
end
if var == 'declined'
'Declined'
end
end
现在的问题是:添加 "return" 或 "next" 是通常的做法吗?有了它,它 运行...但是我从来没有在网上找到必须使用 next 语句的示例...
那么,"if logic" 通常是在其他地方,并且在路线的尽头只有一个 erb :xyz 吗?
我很困惑...
你基本上已经有了答案。你总是需要发送一些东西到货架上才能得到回应。
您可能有一个显示状态的视图,然后在末尾添加类似这样的内容(您可以有多个 erb 块,只需为每个路由添加一个 erb 调用):
get '/' do
var='confirmed'
if var == 'confirmed'
st = 'Confirmed'
end
if var == 'declined'
st = 'Declined'
end
erb :myViewName, :locals => {:status => st}
end
或者像这样使用 return
,如果您的响应只是一个字符串。请注意,此 return
之后的所有内容都不会执行:
if var == 'confirmed'
return 'Confirmed'
end
这与 Sinatra 的工作方式无关,真的。这更像是 Ruby 的事情。根据 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.
您代码中的问题是您的最后一个 if
本身就是一个语句。如果您的 var
变量不是 "declined"
,则 if
块的计算结果为 nil
,因为它是路由块中的最后一个值,所以这就是 if
块的计算结果 nil
=34=] 由 Sinatra 编辑。
当您使用显式 return
时,您不会进入第二个 if
并且不会遇到此问题,这就是它与显式 return 一起使用的原因。
您不需要像这样带有 if/elsif
块的显式 return:
# 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
或一个case/when
块:
# 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
我有一个非常简单的示例,其中 sinatra 只是 returns 没有输出。 程序进入 if 子句但块未完成,因此没有任何内容发送到机架,没有任何内容发送到浏览器...不是单个字符。
require 'sinatra'
get '/' do
var='confirmed'
if var == 'confirmed'
'Confirmed'
end
if var == 'declined'
'Declined'
end
end
现在的问题是:添加 "return" 或 "next" 是通常的做法吗?有了它,它 运行...但是我从来没有在网上找到必须使用 next 语句的示例...
那么,"if logic" 通常是在其他地方,并且在路线的尽头只有一个 erb :xyz 吗?
我很困惑...
你基本上已经有了答案。你总是需要发送一些东西到货架上才能得到回应。
您可能有一个显示状态的视图,然后在末尾添加类似这样的内容(您可以有多个 erb 块,只需为每个路由添加一个 erb 调用):
get '/' do
var='confirmed'
if var == 'confirmed'
st = 'Confirmed'
end
if var == 'declined'
st = 'Declined'
end
erb :myViewName, :locals => {:status => st}
end
或者像这样使用 return
,如果您的响应只是一个字符串。请注意,此 return
之后的所有内容都不会执行:
if var == 'confirmed'
return 'Confirmed'
end
这与 Sinatra 的工作方式无关,真的。这更像是 Ruby 的事情。根据 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.
您代码中的问题是您的最后一个 if
本身就是一个语句。如果您的 var
变量不是 "declined"
,则 if
块的计算结果为 nil
,因为它是路由块中的最后一个值,所以这就是 if
块的计算结果 nil
=34=] 由 Sinatra 编辑。
当您使用显式 return
时,您不会进入第二个 if
并且不会遇到此问题,这就是它与显式 return 一起使用的原因。
您不需要像这样带有 if/elsif
块的显式 return:
# 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
或一个case/when
块:
# 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