# Simply Getting Post Params

**URL:** <https://www.ruby-forum.com/t/simply-getting-post-params/90252>\
**Category:** Rails\
**Created:** [March 29, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/simply-getting-post-params/90252 "2006-03-29T21:59:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmccray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dmccray](https://www.ruby-forum.com/u/dmccray)\
**Post date:** [March 29, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/simply-getting-post-params/90252/1 "2006-03-29T21:59:13Z")

</div>

I am a complete Rails newbie and am trying to understand how I can  
simply get the data from a password field in a form that is not mapped  
to a field in the database.

The scenario is this. I have a User model that is mapped to a table  
users with the following fields: username, email\_address, password and  
join\_dt. Following pretty closely to the login section in the Agile Web  
Development with Rails book, the model takes the username, email\_address  
and password (which is hashed before\_create()) from the form found in  
the add\_user view. It also makes join\_dt = Time.now() in the model.  
There is an associated add\_user action in the login\_controller that  
passes the params to User.new() when a new user is created.

My question is that I want the password to be verified and entered twice  
to ensure the user types the correct password. To do this I thought of  
adding a password\_field(“validate\_pass”,“pass”) to the form in the view.  
All the other form helpers are mapped to the “user” object. (i.e.  
text\_field(“user”, “username”). I then wanted to add a validate method  
in the User model to check that self.password == this validated  
password. However, I am not sure how to access the POST parameter in the  
Model.

Can someone please explain to me what I am doing wrong or even if this  
type of validation should be done in the Model. I thought of adding this  
validation in the login\_controller in the add\_user method as well.  
However, I am a bit confused at how to access this params hash even in  
the controller.

Please help and don’t mind my newbish question if the solution is a bit  
obvious.

Thanks,  
Derrick

---

<div class="post-metadata">

**Author:** ![dmccray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dmccray](https://www.ruby-forum.com/u/dmccray)\
**Post date:** [March 29, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/simply-getting-post-params/90252/2 "2006-03-29T22:07:16Z")

</div>

if the password field is also associated with user then you can find it  
using:  
params[:user][:password]

---

<div class="post-metadata">

**Author:** ![dmccray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dmccray](https://www.ruby-forum.com/u/dmccray)\
**Post date:** [March 29, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/simply-getting-post-params/90252/3 "2006-03-29T22:08:26Z")

</div>

I now see that you are using password\_field(“validate\_pass”,“pass”) so  
you could get this value with:

params[:validate\_pass][:password]

---

<div class="post-metadata">

**Author:** ![dmccray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dmccray](https://www.ruby-forum.com/u/dmccray)\
**Post date:** [March 29, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/simply-getting-post-params/90252/4 "2006-03-29T22:48:26Z")

</div>

Gregory S. wrote:

> I now see that you are using password\_field(“validate\_pass”,“pass”) so  
> you could get this value with:
> 
> params[:validate\_pass][:password]

Thanks for your help.

Would this be able to be called in the model or do I have to call it in  
the controller with the add\_user action?

Derrick

---

<div class="post-metadata">

**Author:** ![dmccray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dmccray](https://www.ruby-forum.com/u/dmccray)\
**Post date:** [March 29, 2006, 11:15pm UTC](https://www.ruby-forum.com/t/simply-getting-post-params/90252/5 "2006-03-29T23:15:16Z")

</div>

On 3/29/06, Gregory S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I now see that you are using password\_field(“validate\_pass”,“pass”) so  
> you could get this value with:
> 
> params[:validate\_pass][:password]

Derrick, to along with what Gregory already wrote you can find the  
correct “path” to your params by looking in your  
[railsapp]/log/development.log file. Each request has its parameters  
recorded like this:

Parameters: {“action”=\>“foo”, “controller”=\>“bar”, “flintstones” =\>  
{“dad” =\> “fred”, “mom” =\> “wilma”} }

Then you can figure out how to access your params.

params[:action]  
params[:flintstones][:dad]  
etc…

– James

---

<div class="post-metadata">

**Author:** ![dmccray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dmccray](https://www.ruby-forum.com/u/dmccray)\
**Post date:** [March 29, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/simply-getting-post-params/90252/6 "2006-03-29T23:17:26Z")

</div>

Derrick McCray wrote:

> Gregory S. wrote:
> 
> > I now see that you are using password\_field(“validate\_pass”,“pass”) so  
> > you could get this value with:
> > 
> > params[:validate\_pass][:password]
> 
> Thanks for your help.
> 
> Would this be able to be called in the model or do I have to call it in  
> the controller with the add\_user action?
> 
> Derrick

Simple answer: in the controller.

Longer answer:  
When you use text\_field(“user”, “name”) or other such rails methods the  
generated html looks something like this:

When rails gets a hold of the post/get parameters it turns user[name]  
and other similarly named fields into a hash and stores it in the params  
hash so that you can retrieve it like this:

user\_hash = params[:user]  
user\_hash[:name]  
…etc  
or more simply  
params[:user][:name]

The model classes by extending from ActiveRecord::Base are designed to  
take one of these hashes as a parameter to their initialize method.  
This means you can write code like:

user = User.new(params[:user])

Though this isn’t the only option. You could also write code like:

data = Hash.new  
data[:name] =\> “Me”  
data[:password] =\> “asdf”  
…  
user = User.new(data)

So in one of your actions you could do this:  
def add\_user  
user = User.new(params[:user])  
if user.password != validate(params[:validate\_pass][:pass])  
flash[:notify] = “Invalid password”  
else  
redirect\_to :action =\> some\_other\_action  
end  
end

---

<div class="post-metadata">

**Author:** ![dmccray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dmccray](https://www.ruby-forum.com/u/dmccray)\
**Post date:** [March 31, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/simply-getting-post-params/90252/7 "2006-03-31T18:32:02Z")

</div>

> My question is that I want the password to be verified and entered twice  
> to ensure the user types the correct password. To do this I thought of  
> adding a password\_field(“validate\_pass”,“pass”) to the form in the view.  
> All the other form helpers are mapped to the “user” object. (i.e.  
> text\_field(“user”, “username”). I then wanted to add a validate method  
> in the User model to check that self.password == this validated  
> password. However, I am not sure how to access the POST parameter in the  
> Model.

You can use:

validates\_confirmation\_of :text\_password

in model to check that user entered password twice, and in view:

\<%= password\_field ‘user’, ‘text\_password’%\>  
\<%= password\_field ‘user’, ‘text\_password\_confirmation’%\>

Access POST parameters with params hash from controller,  
params[‘some\_param’].

hope this helps,  
Bojan

–  
Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)
