Skip to content

Commit

Permalink
user signup
Browse files Browse the repository at this point in the history
  • Loading branch information
ren7087 committed Jul 29, 2021
1 parent 64b4833 commit f4992a6
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'bcrypt', '~> 3.1.7'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ GEM
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
bcrypt (3.1.16)
bindex (0.8.1)
bootsnap (1.7.5)
msgpack (~> 1.0)
Expand Down Expand Up @@ -177,6 +178,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap (>= 1.4.4)
byebug
jbuilder (~> 2.7)
Expand Down
31 changes: 31 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class UsersController < ApplicationController
def index
@pagy, @users = pagy(User.order(id: :desc), items: 25)
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] = 'ユーザを登録しました。'
redirect_to @user
else
flash.now[:danger] = 'ユーザの登録に失敗しました。'
render :new
end
end

private

def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
7 changes: 7 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module UsersHelper
def gravatar_url(user, options = { size: 80 })
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
"https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
end
end
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class User < ApplicationRecord
before_save { self.email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
validates :email, presence: true, length: { maximum: 255 },
format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i },
uniqueness: { case_sensitive: false }
has_secure_password
end
2 changes: 1 addition & 1 deletion app/views/layouts/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
<ul class="navbar-nav">
<li class="nav-item"><a href="#" class="nav-link">Signup</a></li>
<li class="nav-item"><%= link_to 'Signup', signup_path, class: 'nav-link' %></li>
<li class="nav-item"><a href="#" class="nav-link">Login</a></li>
</ul>
</div>
Expand Down
18 changes: 18 additions & 0 deletions app/views/users/_users.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% if users.any? %>
<ul class="list-unstyled">
<% users.each do |user| %>
<li class="mb-3 d-flex">
<img class="rounded me-2" src="<%= gravatar_url(user, { size: 64 }) %>" alt="">
<div>
<div>
<%= user.name %>
</div>
<div>
<p><%= link_to 'View profile', user_path(user), class: "text-decoration-none" %></p>
</div>
</div>
</li>
<% end %>
</ul>
<%== pagy_bootstrap_nav(@pagy) %>
<% end %>
2 changes: 2 additions & 0 deletions app/views/users/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Users#create</h1>
<p>Find me in app/views/users/create.html.erb</p>
1 change: 1 addition & 0 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'users', users: @users %>
34 changes: 34 additions & 0 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<div class="text-center">
<h1>Sign up</h1>
</div>

<div class="row">
<div class="col-sm-6 offset-sm-3">

<%= form_with(model: @user) do |f| %>
<%= render 'layouts/error_messages', model: f.object %>

<div class="mb-3">
<%= f.label :name, 'Name' %>
<%= f.text_field :name, class: 'form-control' %>
</div>

<div class="mb-3">
<%= f.label :email, 'Email' %>
<%= f.email_field :email, class: 'form-control' %>
</div>

<div class="mb-3">
<%= f.label :password, 'Password' %>
<%= f.password_field :password, class: 'form-control' %>
</div>

<div class="mb-3">
<%= f.label :password_confirmation, 'Confirmation' %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>

<%= f.submit 'Sign up', class: 'btn btn-primary w-100' %>
<% end %>
</div>
</div>
19 changes: 19 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="row">
<aside class="col-sm-4">
<div class="card">
<div class="card-header">
<h3 class="card-title"><%= @user.name %></h3>
</div>
<div class="card-body">
<img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt="">
</div>
</div>
</aside>
<div class="col-sm-8">
<ul class="nav nav-tabs nav-justified mb-3">
<li class="nav-item"><a href="#" class="nav-link">Microposts</a></li>
<li class="nav-item"><a href="#" class="nav-link">Followings</a></li>
<li class="nav-item"><a href="#" class="nav-link">Followers</a></li>
</ul>
</div>
</div>
1 change: 1 addition & 0 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'pagy/extras/bootstrap'
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Rails.application.routes.draw do
root to: 'tasks#index'

get 'signup', to: 'users#new'
resources :users, only: [:index, :show, :create]
resources :tasks
end
11 changes: 11 additions & 0 deletions db/migrate/20210729024242_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateUsers < ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password_digest

t.timestamps
end
end
end
10 changes: 9 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f4992a6

Please sign in to comment.