Forward Advance Learning

Accessing URL Parameters in the Params Hash

The params hash contains all the url and post parameters that are passed to your application. We can hit it in out controller, and in our view to get values from the URL.

Say we hit a url like this:

http://localhost:3000/cats?catname=fluffy&catage=12

we would be able to call params[:catname] to get the catname

We can also create a route to match params. In our routes file, we might put something like:

get 'cats/:id' => 'cats#show'

We can then hit a URL like this:

/cats/123

In the params hash, we now have an id. We can do params[:id] to get the value from the url.

Exercise - Make a calculator

We are going to make a simple calculator, you will be able to give it two numbers, and have it give you the sum.

First create a controller and route. The routes should have space for two numbers. It should be possible to hit a URL like this:

http://localhost:3000/calculate/12/15

Retrieve the values and store in instance variables

Now in your controller, retrieve these from the params hash and store them in instance variables, @number_1 and @number_2

Sum them in the view

Finally create a view. In the view, sum together the two numbers, giving a nice output.

Optional Extension

Try and think of a way you can make this work using a form. You will need to change the route to accomplish this.