Simply Getting Post Params

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

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

I now see that you are using password_field(“validate_pass”,“pass”) so
you could get this value with:

params[:validate_pass][:password]

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

On 3/29/06, Gregory S. [email protected] 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

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

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
→ tools, scripts, tricks from our code lab: http://source.mihelac.org