Commit b85d5d4
Changed files (19)
app
controllers
helpers
javascript
packs
models
views
bin
config
db
test
app/controllers/users_controller.rb
@@ -0,0 +1,33 @@
+class UsersController < ApplicationController
+ # GET /users
+ def index
+ @users = User.all
+ end
+
+ # GET /users/1
+ def show
+ @user = User.find(params[:id])
+ end
+
+ # GET /users/new
+ def new
+ @user = User.new
+ end
+
+ # POST /users
+ def create
+ @user = User.new(user_params)
+
+ if @user.save
+ redirect_to @user, notice: 'User was successfully created.'
+ else
+ render :new
+ end
+ end
+
+ private
+
+ def user_params
+ params.require(:user).permit(:handle)
+ end
+end
app/helpers/application_helper.rb
@@ -1,2 +0,0 @@
-module ApplicationHelper
-end
app/javascript/packs/application.js
@@ -1,8 +1,3 @@
-// This file is automatically compiled by Webpack, along with any other files
-// present in this directory. You're encouraged to place your actual application logic in
-// a relevant structure within app/javascript and only use these pack files to reference
-// that code so it'll be compiled.
-
import Rails from "@rails/ujs"
Rails.start()
app/models/user.rb
@@ -0,0 +1,2 @@
+class User < ApplicationRecord
+end
app/views/users/_form.html.erb
@@ -0,0 +1,22 @@
+<%= form_with(model: user) do |form| %>
+ <% if user.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
+
+ <ul>
+ <% user.errors.each do |error| %>
+ <li><%= error.full_message %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= form.label :handle %>
+ <%= form.text_field :handle %>
+ </div>
+
+ <div class="actions">
+ <%= form.submit %>
+ </div>
+<% end %>
app/views/users/index.html.erb
@@ -0,0 +1,25 @@
+<p id="notice"><%= notice %></p>
+
+<h1>Users</h1>
+
+<table>
+ <thead>
+ <tr>
+ <th>Handle</th>
+ <th colspan="3"></th>
+ </tr>
+ </thead>
+
+ <tbody>
+ <% @users.each do |user| %>
+ <tr>
+ <td><%= user.handle %></td>
+ <td><%= link_to 'Show', user %></td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
+
+<br>
+
+<%= link_to 'New User', new_user_path %>
app/views/users/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New User</h1>
+
+<%= render 'form', user: @user %>
+
+<%= link_to 'Back', users_path %>
app/views/users/show.html.erb
@@ -0,0 +1,8 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <strong>Handle:</strong>
+ <%= @user.handle %>
+</p>
+
+<%= link_to 'Back', users_path %>
bin/test
@@ -1,3 +1,3 @@
#!/bin/sh
-bundle exec rails test
+bundle exec rails test "$@"
config/routes.rb
@@ -1,3 +1,6 @@
Rails.application.routes.draw do
- # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
+ # For details on the DSL available within this file,
+ # see https://guides.rubyonrails.org/routing.html
+ resources :users
+ root "users#index"
end
db/migrate/20210501030808_create_users.rb
@@ -0,0 +1,9 @@
+class CreateUsers < ActiveRecord::Migration[6.1]
+ def change
+ create_table :users do |t|
+ t.string :handle, null: false
+ t.timestamps
+ end
+ add_index :users, :handle, unique: true
+ end
+end
db/schema.rb
@@ -10,6 +10,13 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 0) do
+ActiveRecord::Schema.define(version: 2021_05_01_030808) do
+
+ create_table "users", force: :cascade do |t|
+ t.string "handle", null: false
+ t.datetime "created_at", precision: 6, null: false
+ t.datetime "updated_at", precision: 6, null: false
+ t.index ["handle"], name: "index_users_on_handle", unique: true
+ end
end
test/controllers/users_controller_test.rb
@@ -0,0 +1,28 @@
+require "test_helper"
+
+class UsersControllerTest < ActionDispatch::IntegrationTest
+ setup do
+ @user = users(:billie)
+ end
+
+ test "should get index" do
+ get users_url
+ assert_response :success
+ end
+
+ test "should get new" do
+ get new_user_url
+ assert_response :success
+ end
+
+ test "should create user" do
+ post users_url, params: { user: { handle: 'wycats' } }
+
+ assert_redirected_to user_url(User.last)
+ end
+
+ test "should show user" do
+ get user_url(@user)
+ assert_response :success
+ end
+end
test/fixtures/users.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+mona:
+ handle: monalisa
+
+billie:
+ handle: billie
test/models/user_test.rb
@@ -0,0 +1,13 @@
+require "test_helper"
+
+class UserTest < ActiveSupport::TestCase
+ test "creating a new user" do
+ assert User.create(handle: 'eilish')
+ end
+
+ test "cannot create a duplicate user" do
+ assert_raises do
+ User.create!(handle: 'billie')
+ end
+ end
+end
Gemfile
@@ -1,26 +1,6 @@
source 'https://rubygems.org'
-git_source(:github) { |repo| "https://github.com/#{repo}.git" }
-ruby '3.0.0'
-
-# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
-gem 'rails', '~> 6.1.3', '>= 6.1.3.1'
-# Use sqlite3 as the database for Active Record
-gem 'sqlite3', '~> 1.4'
-# Use Puma as the app server
gem 'puma', '~> 5.0'
-# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
+gem 'rails', '~> 6.1.3'
+gem 'sqlite3', '~> 1.4'
gem 'webpacker', '~> 5.0'
-# Use Active Model has_secure_password
-# gem 'bcrypt', '~> 3.1.7'
-
-group :development, :test do
- # Call 'byebug' anywhere in the code to stop execution and get a debugger console
- gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
-end
-
-group :development do
-end
-
-# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
-gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Gemfile.lock
@@ -61,7 +61,6 @@ GEM
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
builder (3.2.4)
- byebug (11.1.3)
concurrent-ruby (1.1.8)
crass (1.0.6)
erubi (1.10.0)
@@ -142,15 +141,10 @@ PLATFORMS
x86_64-darwin-19
DEPENDENCIES
- byebug
puma (~> 5.0)
- rails (~> 6.1.3, >= 6.1.3.1)
+ rails (~> 6.1.3)
sqlite3 (~> 1.4)
- tzinfo-data
webpacker (~> 5.0)
-RUBY VERSION
- ruby 3.0.0p0
-
BUNDLED WITH
2.2.11
Rakefile
@@ -1,6 +1,3 @@
-# Add your own tasks in files placed in lib/tasks ending in .rake,
-# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
-
require_relative "config/application"
Rails.application.load_tasks
README.md
@@ -1,24 +1,3 @@
-# README
+# Sparkles
-This README would normally document whatever steps are necessary to get the
-application up and running.
-
-Things you may want to cover:
-
-* Ruby version
-
-* System dependencies
-
-* Configuration
-
-* Database creation
-
-* Database initialization
-
-* How to run the test suite
-
-* Services (job queues, cache servers, search engines, etc.)
-
-* Deployment instructions
-
-* ...
+A place to show your appreciation for others.