This is what I did:
views/users/new.html.erb:
<h1>Sign up</h1>
<%= form_for(@user) do |f| %>
<% @f_for_partial = f %>
<%= render 'shared/error_messages' %>
<div class="field">
<%= f.label :name, "Name:"%>
</div>
<div>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email, "Email:" %>
</div>
<div>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password, "Password (at least 6 characters):" %>
</div>
<div>
<%= f.password_field :password %>
</div>
<%= render 'users/password_confirmation_fix' %>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
views/users/_password_confirmation_fix.html.erb:
<%
password_error = @user.errors.full_messages.any? do |error|
error.match /password/i
end
%>
<% if password_error %>
<div class="field_with_errors">
<% end %>
<div class="field ">
<%= @f_for_partial.label :password_confirmation, "Password
confirmation:" %>
</div>
<div>
<%= @f_for_partial.password_field :password_confirmation %>
</div>
<% if password_error %>
</div>
<% end %>
And in the action I did this:
def create # POST requests to '/users' routes here
@user = User.new(params[:user])
if @user.save
flash[:success] = "Welcome to the website."
redirect_to @user #goes to user 'show' page
else
@title = 'Sign up'
#If password errors, then erase passwords.
#If no password errors, redisplay passwords.
if @user.errors.full_messages.any? {|error| error.match
/password/i}
@user.password = ''
@user.password_confirmation = ''
end
render 'new'
end
end