Skip to content

Commit d7dad03

Browse files
committed
Finish user signup
1 parent e8f63b1 commit d7dad03

File tree

9 files changed

+187
-1
lines changed

9 files changed

+187
-1
lines changed

app/assets/stylesheets/custom.scss

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
$gray-medium-light: #eaeaea;
77

8+
@mixin box_sizing {
9+
-moz-box-sizing: border-box;
10+
-webkit-box-sizing: border-box;
11+
box-sizing: border-box;
12+
}
13+
814
/* universal */
915

1016
body {
@@ -96,3 +102,77 @@ footer {
96102
}
97103
}
98104
}
105+
106+
/* miscellaneous */
107+
108+
.debug_dump {
109+
clear: both;
110+
float: left;
111+
width: 100%;
112+
margin-top: 45px;
113+
}
114+
115+
/* sidebar */
116+
117+
aside {
118+
section.user_info {
119+
margin-top: 20px;
120+
}
121+
section {
122+
padding: 10px 0;
123+
margin-top: 20px;
124+
&:first-child {
125+
border: 0;
126+
padding-top: 0;
127+
}
128+
span {
129+
display: block;
130+
margin-bottom: 3px;
131+
line-height: 1;
132+
}
133+
h1 {
134+
font-size: 1.4em;
135+
text-align: left;
136+
letter-spacing: -1px;
137+
margin-bottom: 3px;
138+
margin-top: 0px;
139+
}
140+
}
141+
}
142+
143+
.gravatar {
144+
float: left;
145+
margin-right: 10px;
146+
}
147+
148+
.gravatar_edit {
149+
margin-top: 15px;
150+
}
151+
152+
/* forms */
153+
154+
input, textarea, select, .uneditable-input {
155+
border: 1px solid #bbb;
156+
width: 100%;
157+
margin-bottom: 15px;
158+
@include box_sizing;
159+
}
160+
161+
input {
162+
height: auto !important;
163+
}
164+
165+
#error_explanation {
166+
color: red;
167+
ul {
168+
color: red;
169+
margin: 0 0 30px 0;
170+
}
171+
}
172+
173+
.field_with_errors {
174+
@extend .has-error;
175+
.form-control {
176+
color: $state-danger-text;
177+
}
178+
}

app/controllers/users_controller.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
class UsersController < ApplicationController
2+
3+
def show
4+
@user = User.find(params[:id])
5+
end
6+
27
def new
8+
@user = User.new
9+
end
10+
11+
def create
12+
@user = User.new(user_params)
13+
if @user.save
14+
flash[:success] = "Welcome to the Sample App!"
15+
redirect_to @user
16+
else
17+
render 'new', status: :unprocessable_entity
18+
end
319
end
20+
21+
private
22+
23+
def user_params
24+
params.require(:user).permit(:name, :email, :password,
25+
:password_confirmation)
26+
end
427
end

app/helpers/users_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
module UsersHelper
2+
3+
# Returns the Gravatar for the given user.
4+
def gravatar_for(user)
5+
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
6+
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
7+
image_tag(gravatar_url, alt: user.name, class: "gravatar")
8+
end
29
end

app/views/layouts/application.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
<body>
1414
<%= render 'layouts/header' %>
1515
<div class="container">
16+
<% flash.each do |message_type, message| %>
17+
<div class="alert alert-<%= message_type %>"><%= message %></div>
18+
<% end %>
1619
<%= yield %>
1720
<%= render 'layouts/footer' %>
21+
<%= debug(params) if Rails.env.development? %>
1822
</div>
1923
</body>
2024
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<% if @user.errors.any? %>
2+
<div id="error_explanation">
3+
<div class="alert alert-danger">
4+
The form contains <%= pluralize(@user.errors.count, "error") %>.
5+
</div>
6+
<ul>
7+
<% @user.errors.full_messages.each do |msg| %>
8+
<li><%= msg %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>

app/views/users/new.html.erb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
<% provide(:title, 'Sign up') %>
22
<h1>Sign up</h1>
3-
<p>This will be a signup page for new users.</p>
3+
4+
<div class="row">
5+
<div class="col-md-6 col-md-offset-3">
6+
<%= form_with(model: @user) do |f| %>
7+
<%= render 'shared/error_messages' %>
8+
9+
<%= f.label :name %>
10+
<%= f.text_field :name, class: 'form-control' %>
11+
12+
<%= f.label :email %>
13+
<%= f.email_field :email, class: 'form-control' %>
14+
15+
<%= f.label :password %>
16+
<%= f.password_field :password, class: 'form-control' %>
17+
18+
<%= f.label :password_confirmation, "Confirmation" %>
19+
<%= f.password_field :password_confirmation, class: 'form-control' %>
20+
21+
<%= f.submit "Create my account", class: "btn btn-primary" %>
22+
<% end %>
23+
</div>
24+
</div>

app/views/users/show.html.erb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<% provide(:title, @user.name) %>
2+
<div class="row">
3+
<aside class="col-md-4">
4+
<section class="user_info">
5+
<h1>
6+
<%= gravatar_for @user %>
7+
<%= @user.name %>
8+
</h1>
9+
</section>
10+
</aside>
11+
</div>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
get "/about", to: "static_pages#about"
55
get "/contact", to: "static_pages#contact"
66
get "/signup", to: "users#new"
7+
resources :users
78
end

test/integration/users_signup_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require "test_helper"
2+
3+
class UsersSignupTest < ActionDispatch::IntegrationTest
4+
5+
test "invalid signup information" do
6+
get signup_path
7+
assert_no_difference 'User.count' do
8+
post users_path, params: { user: { name: "",
9+
email: "user@invalid",
10+
password: "foo",
11+
password_confirmation: "bar" } }
12+
end
13+
assert_template 'users/new'
14+
end
15+
16+
test "valid signup information" do
17+
get signup_path
18+
assert_difference 'User.count', 1 do
19+
post users_path, params: { user: { name: "Example User",
20+
21+
password: "password",
22+
password_confirmation: "password" } }
23+
end
24+
follow_redirect!
25+
assert_template 'users/show'
26+
end
27+
end

0 commit comments

Comments
 (0)