Daniel Teng wrote:
Thanks! I was a bit confused, being a newcomer to the environment, when
I expected the syntax to read something more like:
@user = @params[:user]
@password = @user.password
I didn’t realize that the password was represented as a hash within the
I wouldn’t say “the password was represented as a hash within the user
password’s not a hash, it’s a field within user.
But, if you look at the generated html for your login page, you’ll see
that the “name”
attribute on the input tag for your password field is “user[password]”.
tells rails to create a hash under the key “user” in the params hash.
In that hash, there are string values under keys like “password”,
“login”, etc. These are
the values that are going to go in the user object. So, that notation is
a way of grouping
together the input params that are going to go into some model object.
And, it allows one to say things like “User.new(@params[:user])” because
everything in the “user” param hash into the constructor (which can be a
btw… someone can start playing around with submitting
And in the case you were looking at, the password was submitted as
rails puts that in a :user hash, so you need to do
@params[:user][:password] to get the
user hash and then the value for password within that.
Hope that makes things a little clearer.