gpt4 book ai didi

ruby - 为什么这个 sinatra 代码不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 17:57:00 24 4
gpt4 key购买 nike

我很难弄清楚我在这里做错了什么。结果为空,我希望它返回 hello(通过 before 帮助程序调用方法 testing)。

require 'rubygems'
require 'sinatra'

get '/' do
end

before do
testing
end

def testing
return "hello"
end

最佳答案

这里有几个问题。一方面,您必须在 View 中实际调用您想要的输出或变量,最典型的是作为实例变量(否则每个用户都会得到相同的输出)。以修改后的代码为例:

require 'rubygems'
require 'sinatra'

get '/' do
@word
end

before do
testing
end

def testing
@word = "hello"
end

查看 Sinatra Book ,一个免费的在线资源,提供有关 Sinatra 入门的信息。

关于ruby - 为什么这个 sinatra 代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11661609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com