add user scaffold

This commit is contained in:
João Victor Geonizeli
2022-02-27 14:46:30 -03:00
parent cec57e0ad2
commit 011a8cdb77
16 changed files with 163 additions and 19 deletions

View File

@@ -0,0 +1,53 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ show update destroy ]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
if @user.save
render :show, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
if @user.update(user_params)
render :show, status: :ok, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:username)
end
end

10
app/models/user.rb Normal file
View File

@@ -0,0 +1,10 @@
class User < ApplicationRecord
validates :username,
presence: true,
uniqueness: true,
length: { maximum: 14 },
allow_nil: false,
format: {
with: /\A[a-zA-Z0-9]+\z/,
}
end

View File

@@ -0,0 +1,2 @@
json.extract! user, :id, :username, :created_at, :updated_at
json.url user_url(user, format: :json)

View File

@@ -0,0 +1 @@
json.array! @users, partial: "users/user", as: :user

View File

@@ -0,0 +1 @@
json.partial! "users/user", user: @user