Ruby Practice Sinatra Basics HTML Forms HTML Forms

silva celso
silva celso
7,059 Points

The route block should take the request parameter named "string", call the reverse method on it, and return the result.

I'm a bit confused about this question Any help will be much appreciated,

app.rb
require "sinatra"

get "/input" do  
  erb :input
end 

post "/reverse" do 

end 
views/input.erb
<form method="post" action="/reverse" >
    <fieldset>
    <label for="info">Info</label>
    <input type="text" id="info" name="string">
  </fieldset>
    <input type="submit">
<form>

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

From Task 2:

The route block should take the request parameter named "string", call the reverse method on it, and return the result.

post "/reverse" do 
  # Your code goes here!
  # Whatever the last expression within this block is,
  # that will be the block's return value,
  # and that's what the browser will display.
  # Like this:
  "This will be the block return value, which the browser will display."
end 

Now, of course, you don't want the block to return "This will be the block return value, which the browser will display.", you want it to return the reversed "string" parameter.

You can access that parameter like this:

params["string"]

params["string"] will return the string that was submitted from the form. Every Ruby string has a reverse method you can call on it, which returns the same string, but backwards:

puts "hello".reverse # prints "olleh"

Call reverse on the string returned from params["string"], and return the result from your Sinatra route block, and you should pass the challenge!