diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 8689c9c..d7b2067 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -9,4 +9,14 @@ def new
@user = User.new
@title = "Sign up"
end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ redirect_to @user, :flash => { :success => "Welcome to the Sample App!" }
+ else
+ @title = "Sign up"
+ render 'new'
+ end
+ end
end
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 75d06d3..dd79b56 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -9,6 +9,9 @@
<%= render 'layouts/header' %>
+ <% flash.each do |key, value| %>
+ <%= value %>
+ <% end %>
<%= yield %>
<%= render 'layouts/footer' %>
diff --git a/app/views/shared/_error_messages.html.erb b/app/views/shared/_error_messages.html.erb
new file mode 100644
index 0000000..d9f1ff0
--- /dev/null
+++ b/app/views/shared/_error_messages.html.erb
@@ -0,0 +1,15 @@
+<% if @user.errors.any? %>
+
+
+ <%= pluralize(@user.errors.count, "error") %>
+ prohibited this user from being saved:
+
+
There were problems with the following fields:
+
+ <% @user.errors.full_messages.each do |message| %>
+ - <%= message %>
+ <% end %>
+
+
+
+<% end %>
\ No newline at end of file
diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb
index a71834e..613ed01 100644
--- a/app/views/users/new.html.erb
+++ b/app/views/users/new.html.erb
@@ -1,6 +1,7 @@
Sign up
<%= form_for(@user) do |f| %>
+ <%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
diff --git a/public/stylesheets/custom.css b/public/stylesheets/custom.css
index b7986ca..115cdfe 100644
--- a/public/stylesheets/custom.css
+++ b/public/stylesheets/custom.css
@@ -138,4 +138,47 @@ td.sidebar {
div.field, div.actions {
margin-bottom: 10px;
-}
\ No newline at end of file
+}
+
+/* Error messages */
+
+.field_with_errors {
+ margin-top: 10px;
+ padding: 2px;
+ background-color: red;
+ display: table;
+}
+
+.field_with_errors label {
+ color: #fff;
+}
+
+#error_explanation {
+ width: 400px;
+ border: 2px solid red;
+ padding: 7px;
+ padding-bottom: 12px;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+}
+
+#error_explanation h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ background-color: #c00;
+ color: #fff;
+}
+
+#error_explanation p {
+ color: #333;
+ margin-bottom: 0;
+ padding: 5px;
+}
+
+#error_explanation ul li {
+ font-size: 12px;
+ list-style: square;
+}
diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb
index 6b1afb9..a239aa9 100644
--- a/spec/controllers/users_controller_spec.rb
+++ b/spec/controllers/users_controller_spec.rb
@@ -53,4 +53,55 @@
response.should have_selector('title', :content => "Sign up")
end
end
+
+ describe "POST 'create'" do
+
+ describe "failure" do
+
+ before(:each) do
+ @attr = { :name => "", :email => "", :password => "",
+ :password_confirmation => "" }
+ end
+
+ it "should have the right title" do
+ post :create, :user => @attr
+ response.should have_selector('title', :content => "Sign up")
+ end
+
+ it "should render the 'new' page" do
+ post :create, :user => @attr
+ response.should render_template('new')
+ end
+
+ it "should not create a user" do
+ lambda do
+ post :create, :user => @attr
+ end.should_not change(User, :count)
+ end
+ end
+
+ describe "success" do
+
+ before(:each) do
+ @attr = { :name => "New User", :email => "user@example.com",
+ :password => "foobar", :password_confirmation => "foobar" }
+ end
+
+ it "should create a user" do
+ lambda do
+ post :create, :user => @attr
+ end.should change(User, :count).by(1)
+ end
+
+ it "should redirect to the user show page" do
+ post :create, :user => @attr
+ response.should redirect_to(user_path(assigns(:user)))
+ end
+
+ it "should have a welcome message" do
+ post :create, :user => @attr
+ flash[:success].should =~ /welcome to the sample app/i
+ end
+ end
+ end
end
diff --git a/spec/requests/users_spec.rb b/spec/requests/users_spec.rb
new file mode 100644
index 0000000..7b62d94
--- /dev/null
+++ b/spec/requests/users_spec.rb
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+describe "Users" do
+
+ describe "signup" do
+
+ describe "failure" do
+ it "should not make a new user" do
+ lambda do
+ visit signup_path
+ fill_in "Name", :with => ""
+ fill_in "Email", :with => ""
+ fill_in "Password", :with => ""
+ fill_in "Confirmation", :with => ""
+ click_button
+ response.should render_template('users/new')
+ response.should have_selector('div#error_explanation')
+ end.should_not change(User, :count)
+ end
+ end
+
+ describe "success" do
+ it "should make a new user" do
+ lambda do
+ visit signup_path
+ fill_in "Name", :with => "Example User"
+ fill_in "Email", :with => "user@example.com"
+ fill_in "Password", :with => "foobar"
+ fill_in "Confirmation", :with => "foobar"
+ click_button
+ response.should have_selector('div.flash.success',
+ :content => "Welcome")
+ response.should render_template('users/show')
+ end.should change(User, :count).by(1)
+ end
+ end
+
+ end
+
+end