Commit 0942534b by Quang Vinh Nguyen

Finish user signup

parent 1a2d6a19
...@@ -107,3 +107,86 @@ footer { ...@@ -107,3 +107,86 @@ footer {
} }
} }
} }
/* mixins, variables, etc. */
$gray-medium-light: #eaeaea;
@mixin box_sizing {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
/* miscellaneous */
.debug_dump {
clear: both;
float: left;
width: 100%;
margin-top: 45px;
@include box_sizing;
}
/* side bar */
aside {
section.user_info {
margin-top: 20px;
}
section {
padding: 10px 0;
margin-top: 20px;
&:first-child {
border: 0;
padding-top: 0;
}
span {
display: block;
margin-bottom: 3px;
line-height: 1;
}
h1 {
font-size: 3em;
text-align: left;
letter-spacing: -1px;
margin-bottom: 3px;
margin-top: 0px;
}
}
}
.gravatar {
float: left;
margin-right: 10px;
}
.gravatar_edit {
margin-top: 15px;
}
/* forms */
input, textarea, select, .uneditable-input {
border: 1px solid #bbb;
width: 100%;
margin-bottom: 15px;
@include box_sizing;
}
input {
height: auto !important;
}
#error_explanation {
color: red;
ul {
color: red;
margin: 0 0 30px 0;
}
}
.field_with_errors {
@extend .has-error;
.form-control {
color: $state-danger-text;
}
}
# This is user controller # This is user controller
class UsersController < ApplicationController 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(user_params)
if @user.save
flash[:success] = 'Welcome to the Sample App!'
redirect_to @user
# redirect_to user_url(@user)
else
render 'new'
end
end
end end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
module UsersHelper module UsersHelper
# Returns the Gravatar for the given user.
def gravatar_for(user, options = {size: 80} )
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}s"
image_tag(gravatar_url, alt: user.name, class: 'gravatar')
end
end end
...@@ -9,8 +9,12 @@ ...@@ -9,8 +9,12 @@
<body> <body>
<%= render 'layouts/header' %> <%= render 'layouts/header' %>
<div class="container"> <div class="container">
<% flash.each do |message_type, message| %>
<%= content_tag(:div, message, class: "alert alert-#{message_type}") %>
<% end %>
<%= yield %> <%= yield %>
</div> </div>
<%= render 'layouts/footer' %> <%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</body> </body>
</html> </html>
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@user.errors.count , "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
\ No newline at end of file
<% provide(:title, 'Sign up') %> <% provide(:title, 'Sign up') %>
<h1>Users#new</h1> <h1>Sign up</h1>
<p>Find me in app/views/users/new.html.erb</p>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user, url: signup_path) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
<% provide(:title, @user.name) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= gravatar_for @user, size: 200 %>
<%= @user.name %>
</h1>
</section>
</aside>
</div>
...@@ -4,4 +4,6 @@ Rails.application.routes.draw do ...@@ -4,4 +4,6 @@ Rails.application.routes.draw do
get '/help', to: 'static_pages#help' get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about' get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact' get '/contact', to: 'static_pages#contact'
post '/signup', to: 'users#create'
resources :users
end end
require 'test_helper'
class UsersSignupTest < ActionDispatch::IntegrationTest
test 'invalid signup information' do
get signup_path
assert_select 'form[action="/signup"]'
assert_no_difference 'User.count' do
post signup_path, params: { user: { name: '',
email: 'user@invalid',
password: 'foo',
password_confirmation: 'bar' } }
end
assert_template 'users/new'
assert_select 'div#error_explanation'
assert_select 'div.alert'
end
test 'valid signup information' do
get signup_path
assert_difference 'User.count', 1 do
post signup_path, params: { user: { name: 'Example User',
email: 'user@example.com',
password: 'foobar',
password_confirmation: 'foobar' } }
end
follow_redirect!
assert_template 'users/show'
assert_not flash.empty?
assert_select 'div.alert'
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment