Quick question about @params

I was looking through the loginGenerator code and noticed the following:

Login:
User.authenticate(@params[:user_login], @params[:user_password])

Signup
User.authenticate(@user.login, @params[:user][:password])

The syntax of the call in the signup code confused me…what exactly
does @params[:user][:password] this return to me? Is the params
structure a multi-dimensional array? I would have thought that the
login and signup use of params would be the same.

Thanks in advance

Daniel Teng wrote:

I was looking through the loginGenerator code and noticed the following:

Login:
User.authenticate(@params[:user_login], @params[:user_password])

Signup
User.authenticate(@user.login, @params[:user][:password])

The syntax of the call in the signup code confused me…what exactly
does @params[:user][:password] this return to me? Is the params
structure a multi-dimensional array? I would have thought that the
login and signup use of params would be the same.

Thanks in advance

@params[:user][:password] is a hash within a hash. It says, get me the
value for password in the user hash.

I am not very familiar with the specific example in the LoginGenerator
code. @params[:user_login] is going to return you the value for the key
:user_login in the @params hash.

Gareth

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
user object.

Regards
Daniel

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
user object.

I wouldn’t say “the password was represented as a hash within the user
object”. The
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]”.
That notation
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
that’ll pass
everything in the “user” param hash into the constructor (which can be a
little dangerous
btw… someone can start playing around with submitting
“user[something]” combinations…).

And in the case you were looking at, the password was submitted as
user[password], so
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.

b

That was a great detailed explanation of how it works. Is this behavior
documented somewhere in the official guides? I’d like to know where I
can read up on this before posting to the group.

Daniel

Well, I hate to tell you to buy a book, but Agile Web D. with
Rails is pretty
indispensible when you’re starting out (and I’m just barely past that).
The first chapter
is pretty hand-holding, though I don’t know that it gets into the params
stuff. I have
that and the “Pickaxe” book (Programming Ruby, also by Dave T.). I
gave myself the
time to read large chunks of these and played with examples and it
started to gel. Sorry
if that’s not the most helpful answer… but you just have to give
yourself some time.
That and ask questions here… it’s great to have this active a list!

b