Skip to content

Commit

Permalink
Finished user signup
Browse files Browse the repository at this point in the history
  • Loading branch information
mhartl committed Aug 25, 2010
1 parent c365270 commit bd7a3a8
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<div class="container">
<%= render 'layouts/header' %>
<section class="round">
<% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
<%= yield %>
</section>
<%= render 'layouts/footer' %>
Expand Down
15 changes: 15 additions & 0 deletions app/views/shared/_error_messages.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% if @user.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@user.errors.count, "error") %>
prohibited this user from being saved:
</h2>
<p>There were problems with the following fields:</p>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>

<% end %>
1 change: 1 addition & 0 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
Expand Down
45 changes: 44 additions & 1 deletion public/stylesheets/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,47 @@ td.sidebar {

div.field, div.actions {
margin-bottom: 10px;
}
}

/* 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;
}
51 changes: 51 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 => "[email protected]",
: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
40 changes: 40 additions & 0 deletions spec/requests/users_spec.rb
Original file line number Diff line number Diff line change
@@ -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 => "[email protected]"
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

0 comments on commit bd7a3a8

Please sign in to comment.