Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

User signup complete

  • Loading branch information...
commit 281fb557f2e87a630f772f7e8fc25cfb7c7a8a15 1 parent db6ec69
@danashan authored
View
44 app/assets/stylesheets/custom.css
@@ -132,4 +132,48 @@ td.sidebar {
border: 1px solid #999;
margin-bottom: -15px;
}
+div.field, div.actions {
+ margin-bottom: 10px;
+}
+
+.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;
+}
View
13 app/controllers/users_controller.rb
@@ -11,4 +11,15 @@ def new
@title = "Sign up"
end
-end
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ flash[:success] = "Welcome to the Sample App!"
+ redirect_to @user
+ else
+ @title = "Sign up"
+ render 'new'
+ end
+ end
+ end
+
View
9 app/views/layouts/application.html.erb
@@ -8,9 +8,12 @@
<body>
<div class="container">
<%= render 'layouts/header' %>
- <section class="round">
- <%= yield %>
- </section>
+ <section class="round">
+ <% flash.each do |key, value| %>
+ <div class="flash <%= key %>"><%= value %></div>
+ <% end %>
+ <%= yield %>
+ </section>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
View
12 app/views/shared/_error_messages.html.erb
@@ -0,0 +1,12 @@
+<% 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 |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+<% end %>
View
27 app/views/users/new.html.erb
@@ -1,2 +1,25 @@
-<h1>Users#new</h1>
-<p>Find me in app/views/users/new.html.erb</p>
+<h1>Sign up</h1>
+<%= form_for(@user) do |f| %>
+ <%= render 'shared/error_messages' %>
+<% end %>
+<%= form_for(@user) do |f| %>
+ <div class="field">
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </div>
+ <div class="field">
+ <%= f.label :email %><br />
+ <%= f.text_field :email %>
+ </div>
+ <div class="field">
+ <%= f.label :password %><br />
+ <%= f.password_field :password %>
+ </div>
+ <div class="field">
+ <%= f.label :password_confirmation, "Confirmation" %><br />
+ <%= f.password_field :password_confirmation %>
+ </div>
+ <div class="actions">
+ <%= f.submit "Sign up" %>
+ </div>
+<% end %>
View
1  config/application.rb
@@ -1,5 +1,6 @@
require File.expand_path('../boot', __FILE__)
+
# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
View
64 spec/controllers/users_controller_spec.rb
@@ -5,18 +5,17 @@
describe "GET 'new'" do
- before(:each) do
- get 'new'
- end
+
it "should be successful" do
+ get :new
response.should be_success
end
it "should have the right title" do
- response.should have_selector('title', :content => "Sign up")
-
- end
- end #end new
+ get :new
+ response.should have_selector("title", :content => "Sign up")
+ end
+ end #end new
describe "GET 'show'" do
@@ -48,5 +47,54 @@
response.should have_selector("h1>img", :class => "gravatar")
end
end
+ describe "POST 'create'" do
- end
+ describe "failure" do
+
+ before(:each) do
+ @attr = { :name => "", :email => "", :password => "",
+ :password_confirmation => "" }
+ end
+
+ it "should not create a user" do
+ lambda do
+ post :create, :user => @attr
+ end.should_not change(User, :count)
+ 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
+ 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
View
39 spec/requests/users_spec.rb
@@ -0,0 +1,39 @@
+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 #failure
+
+ 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#success
+end #signup
+end #USers
View
6 webrat.log
@@ -0,0 +1,6 @@
+# Logfile created on Tue Oct 25 12:36:55 -0700 2011 by logger.rb/22285
+REQUESTING PAGE: GET /signup with {} and HTTP headers {}
+REQUESTING PAGE: POST /users with {"commit"=>"Sign up", "utf8"=>"\342\234\223", "user"=>{"name"=>"", "password_confirmation"=>"", "password"=>"", "email"=>""}} and HTTP headers {"HTTP_REFERER"=>"/signup"}
+REQUESTING PAGE: GET /signup with {} and HTTP headers {}
+REQUESTING PAGE: POST /users with {"commit"=>"Sign up", "utf8"=>"\342\234\223", "user"=>{"name"=>"Example User", "password_confirmation"=>"foobar", "password"=>"foobar", "email"=>"user@example.com"}} and HTTP headers {"HTTP_REFERER"=>"/signup"}
+REQUESTING PAGE: GET http://www.example.com/users/1 with {} and HTTP headers {"HTTP_REFERER"=>"/users"}
Please sign in to comment.
Something went wrong with that request. Please try again.