Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finish user signup

End of chapter 7
  • Loading branch information...
commit be268c9b7307b53e9f4dbbe127d93c295409441c 1 parent 266b328
@simonewebdesign authored
View
75 app/assets/stylesheets/custom.css.scss
@@ -4,6 +4,12 @@
$grayMediumLight: #eaeaea;
+@mixin box_sizing {
+ -moz-box-sizing: border-box;
+ -webkit-box-sizing: border-box;
+ box-sizing: border-box;
+}
+
/* universal */
html {
@@ -11,7 +17,7 @@ html {
}
body {
- padding-top: 60px;
+ padding: 60px 30px;
}
section {
@@ -100,3 +106,70 @@ footer {
}
}
}
+
+/* miscellaneous */
+
+.debug_dump {
+ clear: both;
+ float: left;
+ width: 100%;
+ margin-top: 45px;
+ @include box_sizing;
+}
+
+/* sidebar */
+
+aside {
+ section {
+ padding: 10px 0;
+ border-top: 1px solid $grayLighter;
+ &:first-child {
+ border: 0;
+ padding-top: 0;
+ }
+ span {
+ display: block;
+ margin-bottom: 3px;
+ line-height: 1;
+ }
+ h1 {
+ font-size: 1.4em;
+ text-align: left;
+ letter-spacing: -1px;
+ margin-bottom: 3px;
+ margin-top: 0px;
+ }
+ }
+}
+
+.gravatar {
+ float: left;
+ margin-right: 10px;
+}
+
+/* forms */
+
+input, textarea, select, .uneditable-input {
+ border: 1px solid #bbb;
+ width: 100%;
+ padding: 10px;
+ margin-bottom: 15px;
+ @include box_sizing;
+}
+
+input {
+ height: auto !important;
+}
+
+#error_explanation {
+ color: #f00;
+ ul {
+ list-style: none;
+ margin: 0 0 18px 0;
+ }
+}
+
+.field_with_errors {
+ @extend .control-group;
+ @extend .error;
+ }
View
19 app/controllers/users_controller.rb
@@ -1,4 +1,23 @@
class UsersController < ApplicationController
+
+ def show
+ @user = User.find(params[:id])
+ end
+
def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ # Handle a successful save.
+ # render "users/#{@user}"
+ flash[:success] = "Welcome to the Sample App!"
+ redirect_to @user
+ else
+ render 'new'
+ end
end
+
end
View
9 app/helpers/users_helper.rb
@@ -1,2 +1,9 @@
module UsersHelper
-end
+
+ # Returns the Gravatar (http://gravatar.com/) for the given user.
+ def gravatar_for(user)
+ gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
+ gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
+ image_tag(gravatar_url, alt: user.name, class: "gravatar")
+ end
+end
View
7 app/views/layouts/application.html.erb
@@ -14,10 +14,15 @@
<%= render 'layouts/header' %>
<div class="container">
- <%= yield %>
+ <% flash.each do |key, value| %>
+ <div class="alert alert-<%= key %>"><%= value %></div>
+ <% end %>
+ <%= yield %>
</div>
<%= render 'layouts/footer' %>
+<%= debug(params) if Rails.env.development? %>
+
</body>
</html>
View
12 app/views/shared/_error_messages.html.erb
@@ -0,0 +1,12 @@
+<% if @user.errors.any? %>
+ <div id="error_explanation">
+ <div class="alert alert-error">
+ The form contains <%= pluralize(@user.errors.count, "error") %>.
+ </div>
+ <ul>
+ <% @user.errors.full_messages.each do |msg| %>
+ <li>* <%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+<% end %>
View
24 app/views/users/new.html.erb
@@ -1,3 +1,25 @@
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
-<p>Find me in app/views/users/new.html.erb</p>
+
+<div class="row">
+ <div class="span6 offset3">
+ <%= form_for(@user) do |f| %>
+
+ <%= render 'shared/error_messages' %>
+
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+
+ <%= f.label :email %>
+ <%= f.text_field :email %>
+
+ <%= f.label :password %>
+ <%= f.password_field :password %>
+
+ <%= f.label :password_confirmation, "Confirmation" %>
+ <%= f.password_field :password_confirmation %>
+
+ <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
+ <% end %>
+ </div>
+</div>
View
11 app/views/users/show.html.erb
@@ -0,0 +1,11 @@
+<% provide(:title, @user.name) %>
+<div class="row">
+ <aside class="span4">
+ <section>
+ <h1>
+ <%= gravatar_for @user %>
+ <%= @user.name %>
+ </h1>
+ </section>
+ </aside>
+</div>
View
8 config/environments/test.rb
@@ -1,3 +1,5 @@
+
+
SampleApp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
@@ -34,4 +36,10 @@
# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr
+
+ # Speed up tests by lowering BCrypt's cost function.
+ require 'bcrypt'
+ silence_warnings do
+ BCrypt::Engine::DEFAULT_COST = BCrypt::Engine::MIN_COST
+ end
end
View
3  config/routes.rb
@@ -1,6 +1,7 @@
SampleApp::Application.routes.draw do
+ resources :users
- get "users/new"
+# get "users/new"
root to: 'static_pages#home'
View
8 spec/factories.rb
@@ -0,0 +1,8 @@
+FactoryGirl.define do
+ factory :user do
+ name "Michael Hartl"
+ email "michael@example.com"
+ password "foobar"
+ password_confirmation "foobar"
+ end
+end
View
42 spec/requests/user_pages_spec.rb
@@ -8,7 +8,47 @@
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
- it { should have_selector('title', text: full_title('Sign up')) }
+ it { should have_selector('title', text: 'Sign up') }
end
+
+ describe "profile page" do
+ let(:user) { FactoryGirl.create(:user) }
+ before { visit user_path(user) }
+
+ define "should have a h1 with username" do
+ it { should have_selector('h1', text: user.name) }
+ end
+ define "username should be in title" do
+ it { should have_selector('title', text: user.name) }
+ end
+ end
+
+ describe "signup" do
+
+ before { visit signup_path }
+
+ let(:submit) { "Create my account" }
+
+ describe "with invalid information" do
+ it "should not create a user" do
+ expect { click_button submit }.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
+
+ it "should create a user" do
+ expect { click_button submit }.to change(User, :count).by(1)
+ end
+ end
+ end
+
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.