如何在多个帖子上访问参数或获取

时间:2015-11-23 00:11:27

标签: ruby sinatra

我在我的帖子/'startgames'中要求某人提供他们的名字,并且希望能够在我的所有帖子中使用它并获取并且我有任何帮助的问题将被赞赏....
我刚刚开始学习红宝石,甚至更多,所以即使是sinatra,我已经为comp和在线制作了一个tictactoe游戏,我一直在为计算机本身编写这个游戏,因为我开始并且我不断重构,因为我学习新东西所以请不要对我来说很难,因为我确定我会让它变得比需要的更复杂但是它到目前为止工作

require 'sinatra'
# require_relative "trippy_game_functions.rb"
# trippy = Joestrippygame.new 

get '/startgames' do

answerphone = params[:playgame]

erb :startgame, :locals => {:message1 => "Welcome To Joe's Games. Click the game you wish to play ."}
end

post '/startgames' do

answerphone = params[:playgame]

@playername = @params[:name]
if answerphone == "Joe's Trippy Adventure Game"
    erb :trippygame1, :locals => {:message1 => "#{@playername} Chose to Play Joe's Trippy Adventure Game."}

elsif answerphone == "Jade's Safari Adventure Game"
    erb :safari1, :locals => {:message1 => "#{@playername} Chose to play Jade's Safari Adventure Game."}
else
    erb :startgame, :locals => {:message1 => "Thanks For playing Have a Great Day"}
end
end

post '/answerdoor' do 
answerdoor = params[:door]

@playername = @params[:name]
door = rand(2).floor
if answerdoor.include?('Yes') && door == 0
    # erb :answerdoor, :locals => {:answerdoor => "#{playername} Decides to get up and answer the door", :answerdoor2 => " #{playername} your in Luck It's Your good buddy Dave." }
    erb :davehere, :locals => {:daveishere => "Your in Luck ,It's Your Good Buddy Dave.", :message1 => "Whats up #{@playername} #{params[:name]} I'm Thinking about hitting the club up wanna go? "}

elsif   answerdoor.include?('Yes') && door == 1
            # erb :answerdoor, :locals => {:answerdoor => "#{playername} Decides to get up and answer the door", :answerdoor2 => " Oh No its The cops." }
                redirect to ('/police')
elsif   answerdoor.include?('No') && door == 0 
            erb :answerdoorno, :locals => {:answerdoor => "#{@playername} Decides not to answer the door, but it dosen't Really matter cause Dave walks in anyways."}
elsif   answerdoor.include?('No') && door == 1
            # erb :answerdoorno, :locals => {:answerdoor => "#{playername} Decides not to answer the door, but it dosen't Really matter cause its the cops and the bust in."}
                redirect to ('/police')
end
end

get '/police' do
@playername = @params[:name]
erb :police, :locals => {:msg1 => "Fuck I'ts the cops" ,:arrested => "Well damn man. That sucks #{@playername}...." }
end

1 个答案:

答案 0 :(得分:0)

在大多数应用中,您都希望在某个地方保存用户提交的数据,最常见的是SQL(或NoSQL)数据存储。

在这种情况下,您可以使用session对象在import javax.swing.JOptionPane; public class TreeCalc { public static void main(String[] args) { String[] treeTypes = new String[] {"Fir", "Pine", "Spruce"}; int[] desiredYield = new int[treeTypes.length]; double[] decayRate = new double[] {0.07, 0.12, 0.08}; desiredYield = getYield(decayRate, desiredYield, treeTypes); getCalculate(decayRate, desiredYield, treeTypes); printMessage(decayRate, desiredYield, treeTypes); } //Asks user to input # of trees for each tree type public static int[] getYield(double[] decayRate, int[] desiredYield, String[] treeTypes) { int index = 0; for (int i = 0; i < treeTypes.length; i++) { try { desiredYield[index] = Integer.parseInt(JOptionPane.showInputDialog("Please enter your desired yield for: " + treeTypes[i])); } catch (NumberFormatException e) { desiredYield[index] = 0; JOptionPane.showMessageDialog(null, "Error: Please enter your desired yield for " + treeTypes[i]); } if (desiredYield[index] <= 0) { JOptionPane.showMessageDialog(null, "Error: Please enter your desired yield for " + treeTypes[i]); } else { index++; } } return desiredYield; } //Calculates totals and minimums public static int[] getCalculate(double[] decayRate, int[] desiredYield, String[] treeTypes) { int totalSeeds = 0; int totalTrees = 0; int minSeeds = 0; int index = 0; for (int i = 0; i < treeTypes.length; i++) { minSeeds += (desiredYield[index] * (decayRate[index] * 7)) + desiredYield[index]; totalSeeds += minSeeds; totalTrees += desiredYield[index]; } return desiredYield; } public static void printMessage(double[] decayRate, int[] desiredYield, String[] treeTypes) { getCalculate(decayRate, desiredYield, treeTypes); String treeReport = "Tree Type | Minimum Seeds | Total Seeds | Total Trees "; for (int i = 0; i < treeTypes.length; i++) { treeReport += "\n" + treeTypes[i] + " " + minSeeds + " " + totalSeeds + " " + totalTrees; } JOptionPane.showMessageDialog(null, treeReport); } } 的请求之间保留用户名,并使用session[:name] = params[:name]检索。

(有关其他详细信息,请参阅上述链接文档,例如在使用会话哈希之前需要session[:name]。)

但是你最终可能还想要一个数据库,你可以调查Sequel gem(ORM库)。