entity scaffold

This commit is contained in:
2023-01-13 20:50:41 +02:00
parent c46ecb73e0
commit 26ea76c8e9
15 changed files with 272 additions and 1 deletions

View File

@ -0,0 +1,58 @@
class EntitiesController < ApplicationController
before_action :set_entity, only: %i[ show edit update destroy ]
# GET /entities
def index
@entities = Entity.all
end
# GET /entities/1
def show
end
# GET /entities/new
def new
@entity = Entity.new
end
# GET /entities/1/edit
def edit
end
# POST /entities
def create
@entity = Entity.new(entity_params)
if @entity.save
redirect_to @entity, notice: "Entity was successfully created."
else
render :new, status: :unprocessable_entity
end
end
# PATCH/PUT /entities/1
def update
if @entity.update(entity_params)
redirect_to @entity, notice: "Entity was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /entities/1
def destroy
@entity.destroy
redirect_to entities_url, notice: "Entity was successfully destroyed."
end
private
# Use callbacks to share common setup or constraints between actions.
def set_entity
@entity = Entity.find(params[:id])
end
# Only allow a list of trusted parameters through.
def entity_params
params.require(:entity).permit(:name, :ha_id, :type , :member_id)
end
end

View File

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

7
app/models/entity.rb Normal file
View File

@ -0,0 +1,7 @@
class Entity < ApplicationRecord
validate :name , presence: true
validate :ha_id , presence: true
validate :type , presence: true
validate :member_id , presence: true
end

View File

@ -0,0 +1,19 @@
= form_for @entity do |f|
- if @entity.errors.any?
#error_explanation
%h2= "#{pluralize(@entity.errors.count, "error")} prohibited this entity from being saved:"
%ul
- @entity.errors.full_messages.each do |message|
%li= message
.field
= f.label :name
= f.text_field :name
.field
= f.label :ha_id
= f.text_field :ha_id
.field
= f.label :type
= f.text_field :type
.actions
= f.submit 'Save'

View File

@ -0,0 +1,7 @@
%h1 Editing entity
= render 'form'
= link_to 'Show', @entity
\|
= link_to 'Back', entities_path

View File

@ -0,0 +1,25 @@
%h1 Listing entities
%table
%thead
%tr
%th Name
%th Ha
%th Type
%th
%th
%th
%tbody
- @entities.each do |entity|
%tr
%td= entity.name
%td= entity.ha_id
%td= entity.type
%td= link_to 'Show', entity
%td= link_to 'Edit', edit_entity_path(entity)
%td= link_to 'Destroy', entity, method: :delete, data: { confirm: 'Are you sure?' }
%br
= link_to 'New Entity', new_entity_path

View File

@ -0,0 +1,5 @@
%h1 New entity
= render 'form'
= link_to 'Back', entities_path

View File

@ -0,0 +1,15 @@
%p#notice= notice
%p
%b Name:
= @entity.name
%p
%b Ha:
= @entity.ha_id
%p
%b Type:
= @entity.type
= link_to 'Edit', edit_entity_path(@entity)
\|
= link_to 'Back', entities_path