Skip to content

Instantly share code, notes, and snippets.

@mileszs
Forked from illbzo1/An example of the form method from a view
Created December 14, 2011 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mileszs/1475149 to your computer and use it in GitHub Desktop.
Save mileszs/1475149 to your computer and use it in GitHub Desktop.
Working with form methods in Sinatra
<form method="get" action="/input">
<input type="text" name="input" />
<input type="submit" value="Submit" />
</form>
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
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