1

ここで何が間違っているのかを理解するのに苦労しています。結果は空で、返されることを期待しています( ヘルパーを介してhelloメソッドを呼び出します)。testingbefore

require 'rubygems'
require 'sinatra'

get '/' do
end

before do 
  testing
end 

def testing
  return "hello"
end
4

2 に答える 2

2

ここにはいくつかの問題があります。1 つには、ビューで必要な出力または変数を実際に呼び出す必要があります。ほとんどの場合、インスタンス変数として呼び出します (そうしないと、すべてのユーザーが同じ出力を取得します)。たとえば、以下の変更されたコードを見てください。

require 'rubygems'
require 'sinatra'

get '/' do
  @word
end

before do 
  testing
end 

def testing
  @word = "hello"
end

Sinatraを使い始めるための情報については、無料のオンライン リソースである Sinatra Book を参照してください

于 2012-07-26T03:34:34.400 に答える
2

Get 要求で出力を呼び出していないため、Get メソッドに出力を返すように指示する必要があります。カンフーマンが提案したように。または、次のように最小限の Hello World Sinatra アプリを試してください。

#imports
require 'rubygems'
require 'sinatra'

#Get Request on Root ("/")
get '/' do
    "Hello Sinatra World!"
end

また、プログラムをクラスの下に置くと便利なので、次のこともできます。

#imports
require 'rubygems'
require 'sinatra/base'

#My Application Class
class AppName < Sinatra::base
    get '/' do
        'Hello Sinatra World!'
    end
end

AppName.run!

このようにして、これを別のアプリ ファイルとして使用して、他のファイル内にインポートすることもできます。

require 'app_name' #replace this with the name of the physical file

#Run Application "AppName"
AppName.run!
于 2012-07-26T07:08:58.757 に答える