added friends

This commit is contained in:
2025-11-19 10:55:58 +01:00
parent a13e7d504f
commit dffeb043f6
13 changed files with 228 additions and 2 deletions

View File

@ -4,3 +4,18 @@ h1 { @apply font-bold text-2xl; }
h2 { @apply font-bold text-xl; }
ul { @apply list-inside; }
li { @apply list-disc; }
.btn-new { @apply rounded-md px-3.5 py-2.5 bg-blue-600 hover:bg-blue-500 text-white block font-medium; }
.btn-show { @apply w-full sm:w-auto text-center rounded-md px-3.5 py-2.5 bg-gray-100 hover:bg-gray-50 inline-block font-medium; }
.btn-empty { @apply w-full sm:w-auto text-center mt-2 sm:mt-0 sm:ml-2 rounded-md px-3.5 py-2.5 bg-gray-100 hover:bg-gray-50 inline-block font-medium; }
.btn-destroy { @apply w-full sm:w-auto rounded-md px-3.5 py-2.5 text-white bg-red-600 hover:bg-red-500 font-medium cursor-pointer; }
.notice p#notice { @apply py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-md inline-block; }
form input[type="text"],
form input[type="number"]
{
@apply block shadow-sm rounded-md border px-3 py-2 mt-2 w-full;
}
form input[type="submit"] { @apply w-full sm:w-auto rounded-md px-3.5 py-2.5 bg-blue-600 hover:bg-blue-500 text-white inline-block font-medium cursor-pointer; }

View File

@ -0,0 +1,56 @@
class FriendsController < ApplicationController
before_action :set_friend, only: %i[ show edit update destroy ]
def index
@friends = Friend.all
end
def show
end
def new
@friend = Friend.new
end
def edit
end
def create
@friend = Friend.new(friend_params)
respond_to do |format|
if @friend.save
format.html { redirect_to friends_path, notice: "Der Freund wurde gespeichert." }
else
format.html { render :new, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @friend.update(friend_params)
format.html { redirect_to friends_path, notice: "Der Freund wurde angepasst.", status: :see_other }
else
format.html { render :edit, status: :unprocessable_entity }
end
end
end
def destroy
@friend.destroy!
respond_to do |format|
format.html { redirect_to friends_path, notice: "Der Freund wurde gelöscht.", status: :see_other }
end
end
private
def set_friend
@friend = Friend.find(params.expect(:id))
end
def friend_params
params.expect(friend: [ :firstname, :lastname, :email, :telefon, :level ])
end
end

View File

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

3
app/models/friend.rb Normal file
View File

@ -0,0 +1,3 @@
class Friend < ApplicationRecord
validates :firstname, :lastname, :email, :telefon, presence: true
end

View File

@ -0,0 +1,42 @@
<%= form_with(model: friend, class: "contents") do |form| %>
<% if friend.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-md mt-3">
<h2><%= pluralize(friend.errors.count, "error") %> prohibited this friend from being saved:</h2>
<ul class="list-disc ml-6">
<% friend.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= form.label :firstname %>
<%= form.text_field :firstname, class: [{"border-gray-400 focus:outline-blue-600": friend.errors[:firstname].none?, "border-red-400 focus:outline-red-600": friend.errors[:firstname].any?}] %>
</div>
<div class="my-5">
<%= form.label :lastname %>
<%= form.text_field :lastname, class: [{"border-gray-400 focus:outline-blue-600": friend.errors[:lastname].none?, "border-red-400 focus:outline-red-600": friend.errors[:lastname].any?}] %>
</div>
<div class="my-5">
<%= form.label :email %>
<%= form.text_field :email, class: [{"border-gray-400 focus:outline-blue-600": friend.errors[:email].none?, "border-red-400 focus:outline-red-600": friend.errors[:email].any?}] %>
</div>
<div class="my-5">
<%= form.label :telefon %>
<%= form.text_field :telefon, class: [{"border-gray-400 focus:outline-blue-600": friend.errors[:telefon].none?, "border-red-400 focus:outline-red-600": friend.errors[:telefon].any?}] %>
</div>
<div class="my-5">
<%= form.label :level %>
<%= form.number_field :level, class: [{"border-gray-400 focus:outline-blue-600": friend.errors[:level].none?, "border-red-400 focus:outline-red-600": friend.errors[:level].any?}] %>
</div>
<div class="inline">
<%= form.submit %>
</div>
<% end %>

View File

@ -0,0 +1,22 @@
<div id="<%= dom_id friend %>" class="w-full sm:w-auto my-5 space-y-5">
<div>
<strong class="block font-medium mb-1">Firstname:</strong>
<%= friend.firstname %>
</div>
<div>
<strong class="block font-medium mb-1">Lastname:</strong>
<%= friend.lastname %>
</div>
<div>
<strong class="block font-medium mb-1">Email:</strong>
<%= friend.email %>
</div>
<div>
<strong class="block font-medium mb-1">Telefon:</strong>
<%= friend.telefon %>
</div>
<div>
<strong class="block font-medium mb-1">Level:</strong>
<%= friend.level %>
</div>
</div>

View File

@ -0,0 +1,10 @@
<% content_for :title, "Editing friend" %>
<div class="md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Freund bearbeiten</h1>
<%= render "form", friend: @friend %>
<%= link_to "Anzeigen", @friend, class: "btn-show" %>
<%= link_to "Liste", friends_path, class: "btn-show" %>
</div>

View File

@ -0,0 +1,29 @@
<% content_for :title, "Freunde" %>
<div class="w-full notice">
<% if notice.present? %>
<p id="notice"><%= notice %></p>
<% end %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Freunde</h1>
<%= link_to "Neuer Freund", new_friend_path, class: "btn-new" %>
</div>
<div id="friends" class="min-w-full divide-y divide-gray-200 space-y-5">
<% if @friends.any? %>
<% @friends.each do |friend| %>
<div class="flex flex-col sm:flex-row justify-between items-center pb-5 sm:pb-0">
<%= render friend %>
<div class="w-full sm:w-auto flex flex-col sm:flex-row space-x-2 space-y-2">
<%= link_to "Anzeigen", friend, class: "btn-show" %>
<%= link_to "Bearbeiten", edit_friend_path(friend), class: "btn-show" %>
<%= button_to "Löschen", friend, method: :delete, class: "btn-destroy", data: { turbo_confirm: "Sind Sie sich sicher?" } %>
</div>
</div>
<% end %>
<% else %>
<p class="text-center my-10">Bisher kein Freund eingegeben.</p>
<% end %>
</div>
</div>

View File

@ -0,0 +1,7 @@
<% content_for :title, "Neuer Freund" %>
<div class="md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Neuer Freund</h1>
<%= render "form", friend: @friend %>
<%= link_to "Back to friends", friends_path, class: "btn-empty" %>
</div>

View File

@ -0,0 +1,17 @@
<% content_for :title, "Freund anzeigen" %>
<div class="md:w-2/3 w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-md inline-block" id="notice"><%= notice %></p>
<% end %>
<h1 class="font-bold text-4xl">Freund anzeigen</h1>
<%= render @friend %>
<%= link_to "Bearbeiten", edit_friend_path(@friend), class: "btn-show" %>
<%= link_to "Liste anzeigen", friends_path, class: "btn-show" %>
<%= button_to "Löschen", @friend, method: :delete, form_class: "sm:inline-block mt-2 sm:mt-0 sm:ml-2",
class: "btn-destroy",
data: { turbo_confirm: "Are you sure?" } %>
</div>

View File

@ -9,5 +9,7 @@ Rails.application.routes.draw do
get "pages/:page" => "welcome#pages", as: :pages
resources :friends
root "welcome#index"
end

View File

@ -0,0 +1,12 @@
class CreateFriends < ActiveRecord::Migration[8.0]
def change
create_table :friends do |t|
t.string :firstname, null: false
t.string :lastname, null: false
t.string :email, null: false
t.string :telefon, null: false
t.integer :level, default: 5
t.timestamps
end
end
end

11
db/schema.rb generated
View File

@ -10,5 +10,14 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 0) do
ActiveRecord::Schema[8.0].define(version: 2025_11_19_093322) do
create_table "friends", force: :cascade do |t|
t.string "firstname", null: false
t.string "lastname", null: false
t.string "email", null: false
t.string "telefon", null: false
t.integer "level", default: 5
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end