Add users controller
This commit is contained in:
parent
50280ce389
commit
026bda2a99
@ -3,6 +3,7 @@ class ContestsController < ApplicationController
|
||||
|
||||
def index
|
||||
@contests = current_user.contests
|
||||
@user = current_user
|
||||
end
|
||||
|
||||
def show
|
||||
|
36
app/controllers/users_controller.rb
Normal file
36
app/controllers/users_controller.rb
Normal file
@ -0,0 +1,36 @@
|
||||
class UsersController < ApplicationController
|
||||
before_action :set_user, only: %i[ destroy edit update show ]
|
||||
|
||||
def index
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def update
|
||||
if @user.update(user_params)
|
||||
redirect_to @user
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_user
|
||||
@user = User.find(params[:id])
|
||||
end
|
||||
|
||||
def user_params
|
||||
params.expect(user: [ :username, :email_address ])
|
||||
end
|
||||
end
|
2
app/helpers/users_helper.rb
Normal file
2
app/helpers/users_helper.rb
Normal file
@ -0,0 +1,2 @@
|
||||
module UsersHelper
|
||||
end
|
@ -4,4 +4,6 @@ class User < ApplicationRecord
|
||||
has_secure_password
|
||||
|
||||
normalizes :email_address, with: ->(e) { e.strip.downcase }
|
||||
|
||||
validates :username, presence: true, uniqueness: true
|
||||
end
|
||||
|
@ -1,7 +1,9 @@
|
||||
.container.mt-5
|
||||
- if authenticated?
|
||||
.float-end
|
||||
= button_to "Log out", session_path, method: :delete
|
||||
.float-end.ms-3
|
||||
= button_to "Log out", session_path, method: :delete
|
||||
.float-end.mt-1
|
||||
= link_to "Settings", user_path(@user)
|
||||
|
||||
h1 Welcome!
|
||||
|
||||
|
9
app/views/users/_form.html.slim
Normal file
9
app/views/users/_form.html.slim
Normal file
@ -0,0 +1,9 @@
|
||||
= form_with model: user do |form|
|
||||
div
|
||||
= form.label :username
|
||||
= form.text_field :username
|
||||
div
|
||||
= form.label :email_address
|
||||
= form.text_field :email_address
|
||||
div
|
||||
= form.submit
|
4
app/views/users/edit.html.slim
Normal file
4
app/views/users/edit.html.slim
Normal file
@ -0,0 +1,4 @@
|
||||
.container
|
||||
h1 Edit settings
|
||||
|
||||
= render "form", user: @user
|
1
app/views/users/index.html.slim
Normal file
1
app/views/users/index.html.slim
Normal file
@ -0,0 +1 @@
|
||||
.container
|
4
app/views/users/new.html.slim
Normal file
4
app/views/users/new.html.slim
Normal file
@ -0,0 +1,4 @@
|
||||
.container
|
||||
h1 Create a new user
|
||||
|
||||
= render "form", user: @user
|
9
app/views/users/show.html.slim
Normal file
9
app/views/users/show.html.slim
Normal file
@ -0,0 +1,9 @@
|
||||
.container
|
||||
h1 = @user.username
|
||||
|
||||
p
|
||||
|> Email:
|
||||
= @user.email_address
|
||||
|
||||
a.btn.btn-primary.mt-4 href=edit_user_path(@user)
|
||||
| Edit
|
@ -1,7 +1,4 @@
|
||||
Rails.application.routes.draw do
|
||||
get "contests/create"
|
||||
resource :session
|
||||
resources :passwords, param: :token
|
||||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
||||
|
||||
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
|
||||
@ -12,5 +9,8 @@ Rails.application.routes.draw do
|
||||
root "contests#index"
|
||||
|
||||
resources :contests
|
||||
resources :passwords, param: :token
|
||||
resources :puzzles
|
||||
resource :session
|
||||
resources :users
|
||||
end
|
||||
|
5
db/migrate/20250314145912_add_username_to_user.rb
Normal file
5
db/migrate/20250314145912_add_username_to_user.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class AddUsernameToUser < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_column :users, :username, :string
|
||||
end
|
||||
end
|
3
db/schema.rb
generated
3
db/schema.rb
generated
@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_03_13_142853) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_03_14_145912) do
|
||||
create_table "active_storage_attachments", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
@ -67,6 +67,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_13_142853) do
|
||||
t.string "password_digest", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.string "username"
|
||||
t.index ["email_address"], name: "index_users_on_email_address", unique: true
|
||||
end
|
||||
|
||||
|
28
test/controllers/users_controller_test.rb
Normal file
28
test/controllers/users_controller_test.rb
Normal file
@ -0,0 +1,28 @@
|
||||
require "test_helper"
|
||||
|
||||
class UsersControllerTest < ActionDispatch::IntegrationTest
|
||||
test "should get index" do
|
||||
get users_index_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get edit" do
|
||||
get users_edit_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get show" do
|
||||
get users_show_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get new" do
|
||||
get users_new_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get destroy" do
|
||||
get users_destroy_url
|
||||
assert_response :success
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user