Generate User scaffold

bundle exec rails generate scaffold user email:string{64}:uniq
  status:integer
This commit is contained in:
cryptogopher 2023-03-09 20:20:48 +01:00
parent e497d08f75
commit deaf0fa73b
15 changed files with 263 additions and 0 deletions

View File

@ -0,0 +1,58 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ show edit update destroy ]
# GET /users
def index
@users = User.all
end
# GET /users/1
def show
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
def create
@user = User.new(user_params)
if @user.save
redirect_to @user, notice: "User was successfully created."
else
render :new, status: :unprocessable_entity
end
end
# PATCH/PUT /users/1
def update
if @user.update(user_params)
redirect_to @user, notice: "User was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /users/1
def destroy
@user.destroy
redirect_to users_url, notice: "User was successfully destroyed."
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(:email, :status)
end
end

View File

@ -0,0 +1,2 @@
module UsersHelper
end

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

@ -0,0 +1,2 @@
class User < ApplicationRecord
end

View File

@ -0,0 +1,27 @@
<%= form_with(model: user) do |form| %>
<% if user.errors.any? %>
<div style="color: red">
<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>
<%= form.label :email, style: "display: block" %>
<%= form.text_field :email %>
</div>
<div>
<%= form.label :status, style: "display: block" %>
<%= form.number_field :status %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>

View File

@ -0,0 +1,12 @@
<div id="<%= dom_id user %>">
<p>
<strong>Email:</strong>
<%= user.email %>
</p>
<p>
<strong>Status:</strong>
<%= user.status %>
</p>
</div>

View File

@ -0,0 +1,10 @@
<h1>Editing user</h1>
<%= render "form", user: @user %>
<br>
<div>
<%= link_to "Show this user", @user %> |
<%= link_to "Back to users", users_path %>
</div>

View File

@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>
<h1>Users</h1>
<div id="users">
<% @users.each do |user| %>
<%= render user %>
<p>
<%= link_to "Show this user", user %>
</p>
<% end %>
</div>
<%= link_to "New user", new_user_path %>

View File

@ -0,0 +1,9 @@
<h1>New user</h1>
<%= render "form", user: @user %>
<br>
<div>
<%= link_to "Back to users", users_path %>
</div>

View File

@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>
<%= render @user %>
<div>
<%= link_to "Edit this user", edit_user_path(@user) %> |
<%= link_to "Back to users", users_path %>
<%= button_to "Destroy this user", @user, method: :delete %>
</div>

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :users
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
root "users#index"

View File

@ -0,0 +1,11 @@
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :email, null: false, limit: 64
t.integer :status, null: false, default: 0
t.timestamps null: false
end
add_index :users, :email, unique: true
end
end

View File

@ -0,0 +1,48 @@
require "test_helper"
class UsersControllerTest < ActionDispatch::IntegrationTest
setup do
@user = users(:one)
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
assert_difference("User.count") do
post users_url, params: { user: { email: @user.email, status: @user.status } }
end
assert_redirected_to user_url(User.last)
end
test "should show user" do
get user_url(@user)
assert_response :success
end
test "should get edit" do
get edit_user_url(@user)
assert_response :success
end
test "should update user" do
patch user_url(@user), params: { user: { email: @user.email, status: @user.status } }
assert_redirected_to user_url(@user)
end
test "should destroy user" do
assert_difference("User.count", -1) do
delete user_url(@user)
end
assert_redirected_to users_url
end
end

9
test/fixtures/users.yml vendored Normal file
View File

@ -0,0 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
email: MyString
status: 1
two:
email: MyString
status: 1

7
test/models/user_test.rb Normal file
View File

@ -0,0 +1,7 @@
require "test_helper"
class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

43
test/system/users_test.rb Normal file
View File

@ -0,0 +1,43 @@
require "application_system_test_case"
class UsersTest < ApplicationSystemTestCase
setup do
@user = users(:one)
end
test "visiting the index" do
visit users_url
assert_selector "h1", text: "Users"
end
test "should create user" do
visit users_url
click_on "New user"
fill_in "Email", with: @user.email
fill_in "Status", with: @user.status
click_on "Create User"
assert_text "User was successfully created"
click_on "Back"
end
test "should update User" do
visit user_url(@user)
click_on "Edit this user", match: :first
fill_in "Email", with: @user.email
fill_in "Status", with: @user.status
click_on "Update User"
assert_text "User was successfully updated"
click_on "Back"
end
test "should destroy User" do
visit user_url(@user)
click_on "Destroy this user", match: :first
assert_text "User was successfully destroyed"
end
end