views:

26

answers:

2

Hello guys, I come from Grails and I'm new with Ruby-on-Rails.

So, I learned that when you create a form_for with a symbol like :user("key"), when submited the params[] receive a new key with ":user" and the respective value from it:

<% form_for :user do |form| %>
    <fieldset>
      <legend>Password</legend>

      <label for="current_password"> Current password:</label>
      <%= form.password_field :current_password %>
      <br>

      <label for="password"> New password:</label>
      <%= form.password_field :password %>
      <br>

      <label for="password_confirmation"> Confirm password:</label>
      <%= form.password_field :password_confirmation %>
      <br>
    </fieldset>
    <%= submit_tag "Update", :class => "submit" %>
<% end %>

params[:user => {:current_password => 123 , :password => 321 , :password_confirmation => 321}], right?

Well, the problem is that Rails don't understand the :current_password and :password_confirmation , I don't know if the problem is about the Model User don't have those attributes, only name, password and email.

Error: undefined method `password_confirmation=' for #

Is there another way to fix it without creating a attr_accessor for both symbols? I just wanna work with a simple key inside the action like:

if params[:user].password = params[:user].password_confirmation

 do something...
A: 

The params are nested in a hash. So, you'd access it like this:

if params[:user][:password] == params[:user][:password_confirmation]
  # do stuff
end
Dave Pirotte
+1  A: 

params is the Hash class. Actually it's not simple Hash class, it's HashWithIndifferentAccess, which adds possible to access to hash using string and symbol at one time.

What you need is understand http://ruby-doc.org/core/classes/Hash.html and also read some examples at http://guides.rubyonrails.org/

In your example you also using assignment =, not the equal == sign. It's better to read some manuals about ruby before you start to dig in the rails world.

So fixing your error should look like:

if params[:user][:password] == params[:user][:password_confirmation]

Authlogic or Devise gems do it by themself's. Don't reinvent the wheel before you find that you know all the alternatives and stuck behind the wall.

Dmitry Polushkin