Create guests scaffold

This commit is contained in:
Manuel Bustillo 2024-07-11 20:08:26 +02:00
parent 2f8f3404bb
commit 2ed9f98695
13 changed files with 204 additions and 1 deletions

View File

@ -0,0 +1,70 @@
class GuestsController < ApplicationController
before_action :set_guest, only: %i[ show edit update destroy ]
# GET /guests or /guests.json
def index
@guests = Guest.all
end
# GET /guests/1 or /guests/1.json
def show
end
# GET /guests/new
def new
@guest = Guest.new
end
# GET /guests/1/edit
def edit
end
# POST /guests or /guests.json
def create
@guest = Guest.new(guest_params)
respond_to do |format|
if @guest.save
format.html { redirect_to guest_url(@guest), notice: "Guest was successfully created." }
format.json { render :show, status: :created, location: @guest }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @guest.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /guests/1 or /guests/1.json
def update
respond_to do |format|
if @guest.update(guest_params)
format.html { redirect_to guest_url(@guest), notice: "Guest was successfully updated." }
format.json { render :show, status: :ok, location: @guest }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @guest.errors, status: :unprocessable_entity }
end
end
end
# DELETE /guests/1 or /guests/1.json
def destroy
@guest.destroy!
respond_to do |format|
format.html { redirect_to guests_url, notice: "Guest was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_guest
@guest = Guest.find(params[:id])
end
# Only allow a list of trusted parameters through.
def guest_params
params.require(:guest).permit(:first_name, :last_name, :email, :phone)
end
end

View File

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

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

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

View File

@ -0,0 +1,37 @@
<%= form_with(model: guest) do |form| %>
<% if guest.errors.any? %>
<div style="color: red">
<h2><%= pluralize(guest.errors.count, "error") %> prohibited this guest from being saved:</h2>
<ul>
<% guest.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :first_name, style: "display: block" %>
<%= form.text_field :first_name %>
</div>
<div>
<%= form.label :last_name, style: "display: block" %>
<%= form.text_field :last_name %>
</div>
<div>
<%= form.label :email, style: "display: block" %>
<%= form.text_field :email %>
</div>
<div>
<%= form.label :phone, style: "display: block" %>
<%= form.text_field :phone %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>

View File

@ -0,0 +1,22 @@
<div id="<%= dom_id guest %>">
<p>
<strong>First name:</strong>
<%= guest.first_name %>
</p>
<p>
<strong>Last name:</strong>
<%= guest.last_name %>
</p>
<p>
<strong>Email:</strong>
<%= guest.email %>
</p>
<p>
<strong>Phone:</strong>
<%= guest.phone %>
</p>
</div>

View File

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

View File

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

View File

@ -0,0 +1,9 @@
<h1>New guest</h1>
<%= render "form", guest: @guest %>
<br>
<div>
<%= link_to "Back to guests", guests_path %>
</div>

View File

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

View File

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

View File

@ -0,0 +1,12 @@
class CreateGuests < ActiveRecord::Migration[7.1]
def change
create_table :guests do |t|
t.string :first_name
t.string :last_name
t.string :email
t.string :phone
t.timestamps
end
end
end

11
db/schema.rb generated
View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_07_11_175425) do
ActiveRecord::Schema[7.1].define(version: 2024_07_11_180753) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -26,4 +26,13 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_11_175425) do
t.datetime "updated_at", null: false
end
create_table "guests", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Guest, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end