Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

How to access the :index variable from the params hash.

Within the route's block, set the @index instance variable to the value from the index URL parameter.

require "sinatra"

# Loads the signature at the given line index.
def load_signature(index)
  lines = File.readlines("signatures.txt")
  # Parameter is a string; convert to integer
  index = index.to_i
  lines[index]
end

get "/:index/edit" do
  @index = params[":index"]
end

https://teamtreehouse.com/library/building-web-apps-with-sinatra/updating-data/an-edit-form

I thought this was the proper way to access the index variable through params but I cannot get it to work. Any help is greatly appreciated.

1 Answer

You shouldn't use double quotes around :index! You want to use a symbol, not a string.

Simply remove the double quotes around :index. :grin:

Happy coding! :tada:

:dizzy: ~Alex :dizzy:

Thanks Alexander!