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!

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


Bryan Guillen
Bryan Guillen
1,051 Points

ruby on rails params[] hash explained?

Hey TreeHouse,

I am a bit confused on the subject of params hash...

I have looked everywhere for some sort of simple definition. But, I couldn't find anything...

What exactly are params hashes? Also, why would I use them?


    class SessionsController < ApplicationController 

    def create
        user = User.find_by(email: params[:sessions][:email].downcase
        if user && user.authenticate?(params[:sessions][:password])


In the example above, I know that find_by is used in order to retrieve the user, and the tutorial even goes on to include "strong user params". However I am not so clear on why the params are included. By the way, I completely understand hashes and arrays. This is strictly for params.

Thank You, Bryan G.

Alan Matthews
Alan Matthews
10,161 Points

The params comes from the users browser. The values come from a GET query string, POST form data, or the path of the URL. Rails has a to_param method to make the requests into hashes I believe.

http://www.example.com/?foo[string]=bar&foo[other_string]=baz would equal params[:foo] would be a hash and params[:foo][:string] would be "bar" and params[:foo][:other_string] would be "baz".

Link to Rails Guides on params: http://guides.rubyonrails.org/action_controller_overview.html#parameters

Bryan Guillen
Bryan Guillen
1,051 Points

Ah, I see... Thanks Alan... Very Helpful.... Thanks.

2 Answers

For the purposes of coding most of the time, all you need to know is that the 'params' is a hash of key/value pairs assigned to a variable called 'params'. For what you're probably dealing with most of the time, you just need to know it's inside http requests and you can access it in your controllers.

An easy way to see it is to put a p params inside one of your controller methods (I'll let you work out which one), and then you'll be able to see it in your local server readout.

It might look like this:

{"title"=>"Awesome title", "content"=>"Lorem ipsum innocuous but awesome text"}

In one of your controller methods, if you did:

Post.create(title: params["title"], content: params["content"])

...you would create a Post that had a title of "Awesome title" and content of "Lorem ipsum inoccuous but awesome text".

'Strong params' is a bit harder to get your head around, but it's a Railsey thing and I recommend reading the Rails Guides carefully on it and googling for some explanations and then posting a separate thread up if you're still not sure about it.

Ryan Dsouza
Ryan Dsouza
9,388 Points

If the question still exists in your head, try this link. The explanation is as clear as it can be