如何修复Sinatra在/未定义的本地变量或方法上返回NameError的问题

问题描述 投票:0回答:1

我正在尝试将我创建的RPG字符表转换为网页,但是遇到麻烦。

这是cardtricks.rb文件:

require 'sinatra'
class Character
    def initialize(name, major_arcana, pentacles, wands, swords, cups, eyes, dress, hands)
        @name = name
        @major_arcana = major_arcana
        @pentacles = pentacles
        @wands = wands
        @swords = swords
        @cups = cups
        @eyes = eyes
        @dress = dress
        @hands = hands
    end
end

get '/' do
    erb :index, locals: {wizard_wizard: wizard_wizard}
end

这是.erb文件:

Begin!
<% 
#these are the decks we pull from!
$deck_of_52 = ["Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades", "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts", "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs", "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"]
def hand(x)
  $deck_of_52.sample(x)
end
$major_arcana = ["The Fool", "The Magician", "The High Priestess", "The Empress",  "The Emperor", " The Hierophant", "The Lovers", "The Chariot", "Fortitude", "The Hermit", "Wheel of Fortune", "Justice", "The Hanged Man", "Death", "Temperance", "The Devil", "The Tower", "The Star", "The Moon", "The Sun", "Judgement", "The World"]
def majorpull()
  $major_arcana.sample(1)
end
#this begins character creation!
def mainmenu 
    puts "Do you want to make a move, display your current character, or generate a new character?"
    choice = gets.chomp
        if ["make a move", "move"].include?(choice)
        puts $file_id.move
        puts mainmenu
        elsif ["display", "display my character"].include?(choice)
        puts $file_id.display
        puts mainmenu
        elsif ["generate", "generate a new character"].include?(choice)
        puts wizard_wizard
        else
        puts "Not sure what you meant.  Try again!"
        puts mainmenu
        end
end

def hand(x)
  $deck_of_52.sample(x)
end
def burn
        if (2..4).include?(@wands)
            puts hand(1)
        elsif (5..8).include?(@wands)
            puts hand(2)
        elsif (9..11).include?(@wands)
            puts hand(3)
        end
end
def tools
        if (2..4).include?(@pentacles)
            puts hand(1)
        elsif (5..8).include?(@pentacles)
            puts hand(2)
        elsif (9..11).include?(@pentacles)
            puts hand(3)
        end
end
def shock
        if (2..4).include?(@cups)
            puts hand(1)
        elsif (5..8).include?(@cups)
            puts hand(2)
        elsif (9..11).include?(@cups)
            puts hand(3)
        end
end
def commune
      if (2..4).include?(@swords)
            puts hand(1)
        elsif (5..8).include?(@swords)
            puts hand(2)
        elsif (9..11).include?(@swords)
            puts hand(3)
        end
 end
def display
        puts "My name is #{@name}, I bear the #{@major_arcana}.  I have #{@eyes} eyes, #{@dress} clothes, and #{@hands} hands.  My stats are #{@pentacles} of Pentacles, #{@wands} of Wands, #{@swords} of Swords, and #{@cups} of Cups."
 end
def move
        puts "What move do you want to take?  Burn and Brand, Commune with the Beyond, Tools of the Trade, or Shock and Awe?"
        input = gets.chomp.downcase
        if ["burn", "burn and brand"].include?(input)
            puts burn
        elsif ["shock", "shock and awe"].include?(input)
            puts shock
        elsif ["commune", "commune with the beyond"].include?(input)
            puts commune
        elsif ["tools", "tools of the trade"].include?(input)
            puts tools
        else
            puts "Sorry, that didn't make any sense to me.  Care to try again?"
            puts move
    end
    mainmenu
end
#this generates your character for Wizard World
def wizard_wizard
  puts "What is your name?"
  @playername = gets.chomp
  puts "Give an adjective for how your eyes look."
  @playereyes = gets.chomp
  puts "Give an adjective for how your clothes look."
  @playerdress = gets.chomp
  puts "Give an adjective for how your hands look."
  @playerhands = gets.chomp
  puts "We'll now draw your cards for you."
  @playermajor = majorpull
  w = rand(1..11)
  @playerwands = w
  c = rand(1..11)
  @playercups = c
  s = rand(1..11)
  @playerswords = s
  p = rand(1..11)
  @playerpentas = p
  puts "Name the file we'll save your character to!"
  $file_id = gets.chomp.to_i
  $file_id = Character.new(@playername, @playermajor, @playerpentas, @playerwands, @playerswords, @playercups, @playereyes, @playerdress, @playerhands)
  puts "Meet your new character!"
  $file_id.display
  mainmenu
end
puts wizard_wizard
%>

我从Sinatra得到的错误是:

undefined local variable or method `wizard_wizard' for #<Sinatra::Application:0x00007f6508de1470>

我曾尝试提供wizard_wizard范围(@$),但这只会导致更多语法错误。

ruby sinatra game-development
1个回答
0
投票

wizard_wizard未定义。

首先使用sinatra,一个红宝石过程开始,它将加载一般的东西,并且在您的情况下,还定义了Character类。但是执行流程从您定义调用/时应该发生的事情开始:调用wizard_wizard,将键:wizard_wizard赋予locals哈希,然后调用erb解释索引模板。只有这样,才能解析wizard_wizard函数。

在一个单独的文件中定义您的功能,需要时需要该文件。另外,要求终端输入sinatra网站模板吗?那行不通。您不需要在这里使用sinatra-有了它,您似乎必须构建根本不需要框架的终端应用程序,而必须适应Web应用程序的工作方式(HTML,通过表单输入,Javascript)。

© www.soinside.com 2019 - 2024. All rights reserved.