Forked from illbzo1/An example of the form method from a view
Created
December 14, 2011 03:29
-
-
Save mileszs/1475149 to your computer and use it in GitHub Desktop.
Working with form methods in Sinatra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="get" action="/input"> | |
<input type="text" name="input" /> | |
<input type="submit" value="Submit" /> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/input' do | |
@input = params[:input] | |
# I'm assuming this instantiates a TorchRoom object from the class below | |
@location = current_location | |
case @location.class | |
when TorchRoom | |
next_room = @location.move(@input.gsub('go ', '')) | |
if next_room.blank? | |
puts "I don't understand what you just said." | |
erb :torch_room | |
else | |
redirect_to next_room | |
end | |
end | |
end | |
# ... | |
# Some other file, probably, but required | |
# Also, turn input into "north" instead of "go north" somewhere | |
class TorchRoom | |
ADJACENT_ROOMS = { | |
"north" => "dais_room", | |
"northwest" => "chest_room", | |
"northeast" => "orb_room" | |
} | |
def move(direction) | |
next_room = ADJACENT_ROOMS[direction] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/input' do | |
@input = params[:input] | |
end | |
get '/' do | |
@torch_room = torch_room | |
@location == @torch_room # Did you mean assignment here? | |
case @input | |
when "go northwest" | |
redirect "/chest_room" | |
when "go north" | |
redirect "/dais_room" | |
when "go northeast" | |
redirect "/orb_room" | |
else | |
puts "I don't understand what you just said." | |
erb :torch_room | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment