如何访问位于我的Sinatra应用程序前端的session[:name]
,我在后端的课程中?我想这样做,所以我可以将我的所有消息(人们都说)存储在一个类中,以便能够快速更改我的后端消息,以便我可以根据需要更改它而不必转移到整个前端在那里编码。
这是前端部分:
post '/startgames' do
play = params[:playgame]
session[:name] = params[:name]
session[:age] = params[:age].to_i
if play == "Joe's Trippy Adventure Game" && session[:age] > 17
erb :trippygame1, :locals => {:message1 => "Great #{session[:name]} you are old enough to Play Joe's Trippy Adventure Game.",
:message2 => "One day #{session[:name]} is sitting around Smoking a bong when someone knocks on the door.",
:message3 => "Do you get up and answer it?"}
elsif play == "Joe's Trippy Adventure Game" && session[:age] < 17
erb erb :safari1, :locals => {:message1 => "#{session[:name]} You are to young to play Joe's Trippy game how about a nice Lepoard adventure game?", :age => ""}
elsif play == "Jade's Safari Adventure Game" && session[:age] < 17
erb :safari1, :locals => {:message1 => "#{session[:name]} Chose to play Jade's Safari Adventure Game.", :age =>""}
else play == "Jade's Safari Adventure Game" && session[:age] > 17
erb :safari1, :locals => {:age => "Hey man Aren't You a little old to play this type of game?",
:message1 => "#{session[:name]} Chose to play Jade's Safari Adventure Game."}
end
end
这是后端部分:
class Start_messages
def welcome
"Welcome To Joe's Games."
end
def name_age
"Enter your name & age to start."
end
def to_start_jade
"So you wanna play Jade's Safari Adventure game ?"
end
def to_start_joe
"If you want to play Joe's Trippy Adventure Game"
end
def start_joe
"Great #{session[:name]} you are old enough to Play Joe's Trippy Adventure Game."
end
end
我真的希望能够将所有消息保留在一个类中而不是在前端进行硬编码。另外,我会更容易在后端测试它们,因为我还没有开始测试前端的东西......我学习TDD所以测试测试测试:)
答案 0 :(得分:0)
谢谢戴夫你的建议让我走向了正确的方向。这就是我所做的,因为它会帮助别人。这部分是针对后端的
post '/startgames' do
play = params[:playgame]
session[:name] = params[:name]
session[:age] = params[:age].to_i
name = session[:name]
if play == "Joe's Trippy Adventure Game" && session[:age] > 17
erb :trippygame1, :locals => {:message1 => message.start_joe(name),
:message2 => "One day #{session[:name]} is sitting around Smoking a bong when someone knocks on the door.",
:message3 => "Do you get up and answer it?"}
elsif play == "Joe's Trippy Adventure Game" && session[:age] < 17
erb erb :safari1, :locals => {:message1 => message.too_young(name) , :age => ""}
elsif play == "Jade's Safari Adventure Game" && session[:age] < 17
erb :safari1, :locals => {:message1 => message.start_jade(name), :age =>""}
else play == "Jade's Safari Adventure Game" && session[:age] > 17
erb :safari1, :locals => {:age => message.too_old(name),
:message1 => message.start_jade(name)}
end
end
前端。
{{1}}
现在仍在进行中,但感谢现在完全有道理的建议我不知道为什么我没有想到这个