Hello.
I'm trying to create a registration form with Rails. It's working, but it doesn't displays the errors from the validation (it validates, but the errors don't display).
Here are my files:
# new.html.erb
<h1>New user</h1>
<% form_for :user, :url =>{:action=>"new", :controller=>"users"} do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', users_path %>
# user.rb
class User < ActiveRecord::Base
validates_presence_of :name
validates_presence_of :password
end
#users_controller.rb
class UsersController < ApplicationController
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
def new
if session[:user_id].nil?
if params[:user].nil? #User hasn't filled the form
@user = User.new
else #User has filled the form
user = User.new(params[:user])
if user.save
user.salt = rand(1000000000)
user.password = Digest::MD5.hexdigest(user.salt.to_s + user.password)
user.save
flash[:notice] = 'User was successfully created.'
session[:user_id] = user.id
session[:password] = user.password
redirect_to url_for(:action=>"index",:controller=>"users")
else
render :action=>"new"
end
end
else #User is already logged in
flash[:notice] = 'You are already registered.'
redirect_to url_for(:action=>"index")
end
end
# some other actions removed....
end
Why aren't the errors being displayed?
Thanks!!