Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ gem "jbuilder"
# gem "kredis"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"
gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
bcrypt (3.1.18)
bindex (0.8.1)
bootsnap (1.16.0)
msgpack (~> 1.2)
Expand Down Expand Up @@ -209,6 +210,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
capybara
debug
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class UsersController < ApplicationController

def new
@user = User.new
end

def create
user_params = params.require(:user).permit(
:username,
:email,
:password,
:password_confirmation
)

@user = User.new(user_params)

if @user.valid?
render 'new'
else
render 'new', status: :unprocessable_entity
end
end

end
13 changes: 13 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class User < ApplicationRecord
has_secure_password

has_secure_token :email_confirmation_token

validates :username,
format: { with: /\A[A-Za-z0-9_]{2,20}\z/ , message: 'should contain 2 to 20 alphanumeric characters' },
uniqueness: { case_sensitive: false }

validates :email,
format: { with: URI::MailTo::EMAIL_REGEXP },
uniqueness: { case_sensitive: false }
end
27 changes: 25 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<title>App</title>
<title>
<% if content_for?(:page_title) %>
<%= yield :page_title %> | Petbook
<% else %>
Petbook
<% end %>
</title>

<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</head>

<body>
<div class="navbar">
<div class="container">
<a class="navbar-brand" href="<%= root_path %>">Petbook</a>

<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="<%= new_users_path %>">Sign Up</a>
</li>
</ul>
</div>
</div>

<%= yield %>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
<%= javascript_importmap_tags %>
</body>
</html>
54 changes: 54 additions & 0 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<% content_for :page_title, "Sign up" %>

<div class="row">
<div class="col-12 col-lg-4 offset-lg-4">
<div class="card" style="margin-top: 50px">

<div class="card-body">

<div class="card-title">
<h1>Sign Up</h1>
</div>

<div class="card-text">
<% if @user.errors.any? %>
<div class="alert alert-danger">
<p>Your registration couldn't be processed:</p>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<%= form_with model: @user do |f| %>
<div class="mb-3">
<%= f.label :username, "User name" %>
<%= f.text_field :username, class: 'form-control' %>
</div>

<div class="mb-3">
<%= f.label :email, "Email address" %>
<%= 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, "Confirm your password" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>

<div class="mb-3">
<button type="submit" class="btn btn-primary">Sign Up</button>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
# root "articles#index"
root "users#new"

resource :users, only: [:new, :create]
end
16 changes: 16 additions & 0 deletions db/migrate/20230311181749_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :username
t.string :email
t.string :email_confirmed, default: false
t.string :email_confirm_token
t.string :password_digest
t.string :first_name
t.string :last_name
t.boolean :avatar, default: false

t.timestamps
end
end
end
27 changes: 27 additions & 0 deletions db/schema.rb

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

21 changes: 21 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
username: MyString
email: MyString
email_confirmed: MyString
email_confirm_token: MyString
password_digest: MyString
first_name: MyString
last_name: MyString
avatar: false

two:
username: MyString
email: MyString
email_confirmed: MyString
email_confirm_token: MyString
password_digest: MyString
first_name: MyString
last_name: MyString
avatar: false
7 changes: 7 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end