Forum: Ruby on Rails Simply Getting Post 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.
6a7affc93dc8139df371194733b4f1b3?d=identicon&s=25 Derrick Mccray (dmccray)
on 2006-03-29 21:59
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
585efae3145da7a5e3c6daf928471ee7?d=identicon&s=25 Gregory Stickley (pie-r-squared)
on 2006-03-29 22:07
if the password field is also associated with user then you can find it
using:
params[:user][:password]
585efae3145da7a5e3c6daf928471ee7?d=identicon&s=25 Gregory Stickley (pie-r-squared)
on 2006-03-29 22:08
I now see that you are using password_field("validate_pass","pass") so
you could get this value with:

params[:validate_pass][:password]
6a7affc93dc8139df371194733b4f1b3?d=identicon&s=25 Derrick Mccray (dmccray)
on 2006-03-29 22:48
Gregory Stickley 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
119af50160cabfe1fb6f2f05f5018c64?d=identicon&s=25 James Ludlow (Guest)
on 2006-03-29 23:15
(Received via mailing list)
On 3/29/06, Gregory Stickley <gstickley@gusto.com> 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
585efae3145da7a5e3c6daf928471ee7?d=identicon&s=25 Gregory Stickley (pie-r-squared)
on 2006-03-29 23:17
Derrick McCray wrote:
> Gregory Stickley 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:

<input class="textfield" id="user_name" name="user[name]" type="text">

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
866edcb271e88be4bfd935eea7947730?d=identicon&s=25 Bojan Mihelac (Guest)
on 2006-03-31 18:32
(Received via mailing list)
>
> 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 Mihelac
Informatika Mihelac, Bojan Mihelac s.p.  |  www.informatikamihelac.com
-> tools, scripts, tricks from our code lab: http://source.mihelac.org
This topic is locked and can not be replied to.