reader1
reader1

Reputation: 93

Simple string comparison not working! Frustration ensues

I cannot seem to figure out why this comparison is not working:

if params[:password].to_s == params[:conf_password].to_s
  #do something
else
  #do something else

The result is always false, executing the else block... Anyone know why?

Edit: Checking the output shows that the two parameters are identical. "password" is collected using a 'form_for' and "conf_password" from a password_field_tag.

If "conf_password" is included in the 'form_for' a no such method error is thrown, because there is no column conf_password in the model. Perhaps there is a better way of collecting this param, which may solve the issue.

some log output regarding params.

PARAMS: {"password"=>"1234567", "company"=>"company1", "companykey"=>"ckey2"}, "conf_password"=>"1234567", 

Code to get these values

<tr> <td> <%= label_tag(:password, "Password") %> </td> <td> <%= f.password_field :password %> </td> </tr>

<tr> <td> <%= label_tag(:conf_password, "Confirm Password") %> </td> <td> <%= password_field_tag(:conf_password) %> </td> </tr>

Upvotes: 0

Views: 1436

Answers (1)

Aleksi Yrttiaho
Aleksi Yrttiaho

Reputation: 8446

It seems to me that you should not be accessing :password and :conf_password the same way as the :password parameter is within a hash while :conf_password is not.

When you declare the form_for you also define the hash that contains the :password and you have to access the :password-parameter with new Object(params[:object]).password. You are able to access the parameter with params[:object][:password] as well.

Untested example form based on copy-paste coding

<%= form_for @person do |f| %>
  <%= f.label :first_name %>:
  <%= f.text_field :first_name %><br />

  <%= f.label :password %>:
  <%= f.text_field :password %><br />

  <%= f.submit %>
<% end %>

Untested example controller based on deduction not knowledge

if Person.create(params[:person]).password == 'foobar'
   # go on
else 
   # fail
end

For more though out description see the API: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

Upvotes: 3

Related Questions