Permalink
Browse files

Finished Chapter 7

  • Loading branch information...
1 parent fe96fee commit 5d8d9aa13e417959b25a8258ef6e76ee71501e63 @rcherveny committed Feb 21, 2012
View
0 -e
No changes.
@@ -141,4 +141,49 @@ td.sidebar {
padding: 1em;
vertical-align: top;
background: #ffc;
+}
+
+div.field, div.actions {
+ margin-bottom: 10px;
+}
+
+.field_with_errors{
+ margin-top: 10px;
+ padding: 2px;
+ background-color: red;
+ display: table;
+
+ label {
+ color: #fff;
+ }
+}
+
+#error_explanation {
+ width: 400px;
+ border: 2px solid red;
+ padding: 7px;
+ padding-bottom: 12px;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+
+ h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ background-color: #c00;
+ color: #fff;
+ }
+
+ p {
+ color: #333;
+ margin-bottom: 0;
+ padding: 5px;
+ }
+
+ ul li {
+ font-size: 12px;
+ list-style: square;
+ }
}
@@ -1,8 +1,20 @@
class UsersController < ApplicationController
- def new
- end
def show
@user = User.find(params[:id])
end
+
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ flash[:success] = "Welcome to the sample app"
+ redirect_to @user
+ else
+ render 'new'
+ end
+ end
end
@@ -10,6 +10,9 @@
<div class="container">
<%= render 'layouts/header' %>
<section class="round">
+ <% flash.each do |key, value| %>
+ <div class="flash <%= key %>"><%= value %></div>
+ <% end %>
<%= yield %>
</section>
<footer>
@@ -0,0 +1,13 @@
+<% 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 %>
+
@@ -1,3 +1,25 @@
<% provide(:title, 'Sign up') %>
<h1>Sign Up</h1>
-<p>Find me in app/views/users/new.html.erb</p>
+
+<%= form_for @user do |f| -%>
+ <%= render 'shared/error_messages' %>
+ <div class="field">
+ <%= f.label :name %><br /r>
+ <%= 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
@@ -2,4 +2,7 @@
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en:
- hello: "Hello world"
+ activerecord:
+ attributes:
+ user:
+ password_digest: "Password"
@@ -11,12 +11,41 @@
it { should have_selector('title', text: user.name) }
end
-
-
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign Up') }
it { should have_selector('title', text: full_title('Sign up')) }
end
+
+ describe "signup" do
+ before { visit signup_path }
+
+ describe "with invalid information" do
+ it "should not create a user" do
+ expect { click_button "Sign up" }.not_to change(User, :count)
+ end
+ end
+
+ describe "with valid information" do
+ before do
+ fill_in "Name", with: "Example User"
+ fill_in "Email", with: "user@example.com"
+ fill_in "Password", with: "foobar"
+ fill_in "Confirmation", with: "foobar"
+ end
+
+ describe "after saving the user" do
+ before { click_button "Sign up" }
+ let(:user) { User.find_by_email('user@example.com') }
+
+ it { should have_selector('title', text: user.name) }
+ it { should have_selector('div.flash.success', text: 'Welcome') }
+ end
+
+ it "should create a user" do
+ expect { click_button "Sign up" }.to change(User, :count).by(1)
+ end
+ end
+ end
end

0 comments on commit 5d8d9aa

Please sign in to comment.