How to create a password and confirmation when using form_for?

I use form_for, but I'm not sure how to create a password and password confirmation using helpers?

I still:

<%= form_for :user, @user, .... do |f| %> <%= f.text_field :user_name, :class .... %> password?? <% end %> 

In addition, when submitting the / user / create action, how to do it, I prevent the initialization of certain fields in the model when using:

 @user = User.new(params[:user]) 
+6
ruby-on-rails forms
source share
2 answers

Put this in your view form:

 <%= f.password_field :password %> <%= f.password_field :password_confirmation %> 

And this is in your user model:

 validates_confirmation_of :password 

Now, to prevent unwanted initializations in your controller, add the following to your model:

 attr_accessible :attribute1, attribute2 

Now these attributes will be the only attributes that can be set through the so-called mass assignment.

+14
source share

If you have a password database column (of course, you'd better save the salt and the encrypted password), you can do this:

 class User attr_accessor :password_confirmation # Note. You do not need this field in database, it for 1-time use # The following 2 lines let you prevent certain fields attr_accessible :user_name attr_protected :password # Note that if you used attr_accessible, and all other fields cannot be assigned through User.new(params[:user[), while if you used attr_protected, only those fields cannot assigned. validates_confirmation_of :password # This automatically validates if :password == :password_confirmation 

In your opinion:

 <%= f.password_field :password %> <%= f.password_field :password_confirmation %> 
+2
source share

All Articles