Forum: Ruby on Rails Quick question about @params

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel Teng (Guest)
on 2006-03-04 01:53
(Received via mailing list)
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
Gareth R. (Guest)
on 2006-03-04 02:49
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
Daniel Teng (Guest)
on 2006-03-05 07:37
(Received via mailing list)
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
Ben M. (Guest)
on 2006-03-05 09:10
(Received via mailing list)
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
Daniel Teng (Guest)
on 2006-03-07 02:04
(Received via mailing list)
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
Ben M. (Guest)
on 2006-03-07 07:31
(Received via mailing list)
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
This topic is locked and can not be replied to.