diff --git a/app/controllers/tsueets_controller.rb b/app/controllers/tsueets_controller.rb new file mode 100644 index 0000000..66b3113 --- /dev/null +++ b/app/controllers/tsueets_controller.rb @@ -0,0 +1,60 @@ +class TsueetsController < ApplicationController + before_action :set_tsueet, only: %i[ show edit update destroy ] + + # GET /tsueets + def index + @tsueets = Tsueet.order(id: :desc) + @tsueet = Tsueet.new + end + + # GET /tsueets/1 + def show + end + + # GET /tsueets/new + def new + @tsueet = Tsueet.new + end + + # GET /tsueets/1/edit + def edit + end + + # POST /tsueets + def create + @tsueet = Tsueet.new(tsueet_params) + + if @tsueet.save + redirect_to tsueets_path, notice: "Tsueet was successfully created." + else + # TODO: バリデーションエラーのときも一覧ページにエラー表示できるように検討する + render :new, status: :unprocessable_entity + end + end + + # PATCH/PUT /tsueets/1 + def update + if @tsueet.update(tsueet_params) + redirect_to @tsueet, notice: "Tsueet was successfully updated.", status: :see_other + else + render :edit, status: :unprocessable_entity + end + end + + # DELETE /tsueets/1 + def destroy + @tsueet.destroy! + redirect_to tsueets_path, notice: "Tsueet was successfully destroyed.", status: :see_other + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_tsueet + @tsueet = Tsueet.find(params.expect(:id)) + end + + # Only allow a list of trusted parameters through. + def tsueet_params + params.expect(tsueet: [ :content ]) + end +end diff --git a/app/models/tsueet.rb b/app/models/tsueet.rb new file mode 100644 index 0000000..e4783b5 --- /dev/null +++ b/app/models/tsueet.rb @@ -0,0 +1,7 @@ +class Tsueet < ApplicationRecord + validates :content, presence: true + + after_create_commit -> { broadcast_refresh_later_to("tsueets") } + after_update_commit -> { broadcast_refresh_later; broadcast_refresh_later_to("tsueets") } + after_destroy_commit -> { broadcast_refresh; broadcast_refresh_later_to("tsueets") } +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9ca9aa9..7154810 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,6 +10,9 @@ <%= yield :head %> + + + <%# Enable PWA manifest for installable apps (make sure to enable in config/routes.rb too!) %> <%#= tag.link rel: "manifest", href: pwa_manifest_path(format: :json) %> @@ -23,6 +26,9 @@ - <%= yield %> +
+

Tsuitter

+ <%= yield %> +
diff --git a/app/views/tsueets/_form.html.erb b/app/views/tsueets/_form.html.erb new file mode 100644 index 0000000..e86e4ce --- /dev/null +++ b/app/views/tsueets/_form.html.erb @@ -0,0 +1,21 @@ +<%= form_with(model: tsueet, data: { turbo_frame: '_top' }) do |form| %> + <% if tsueet.errors.any? %> +
+

<%= pluralize(tsueet.errors.count, "error") %> prohibited this tsueet from being saved:

+ + +
+ <% end %> + +
+ <%= form.textarea :content, class: "form-control" %> +
+ +
+ <%= form.submit class: "btn btn-primary" %> +
+<% end %> diff --git a/app/views/tsueets/_tsueet.html.erb b/app/views/tsueets/_tsueet.html.erb new file mode 100644 index 0000000..cca2896 --- /dev/null +++ b/app/views/tsueets/_tsueet.html.erb @@ -0,0 +1,5 @@ +
+
+ <%= tsueet.content %> +
+
diff --git a/app/views/tsueets/edit.html.erb b/app/views/tsueets/edit.html.erb new file mode 100644 index 0000000..266bd8a --- /dev/null +++ b/app/views/tsueets/edit.html.erb @@ -0,0 +1,12 @@ +<% content_for :title, "Editing tsueet" %> + +

Editing tsueet

+ +<%= render "form", tsueet: @tsueet %> + +
+ +
+ <%= link_to "Show this tsueet", @tsueet %> | + <%= link_to "Back to tsueets", tsueets_path %> +
diff --git a/app/views/tsueets/index.html.erb b/app/views/tsueets/index.html.erb new file mode 100644 index 0000000..298d389 --- /dev/null +++ b/app/views/tsueets/index.html.erb @@ -0,0 +1,14 @@ +<% content_for :title, "Tsueets" %> +<%= turbo_stream_from 'tsueets' %> + +
+ <%= render 'form', tsueet: @tsueet %> +
+ +
+ <% @tsueets.each do |tsueet| %> +
+ <%= render tsueet %> +
+ <% end %> +
diff --git a/app/views/tsueets/new.html.erb b/app/views/tsueets/new.html.erb new file mode 100644 index 0000000..9c44e54 --- /dev/null +++ b/app/views/tsueets/new.html.erb @@ -0,0 +1,13 @@ +<% content_for :title, "New tsueet" %> + +

New tsueet

+ +<%= turbo_frame_tag 'new_tsueet' do %> + <%= render "form", tsueet: @tsueet %> +<% end %> + +
+ +
+ <%= link_to "Back to tsueets", tsueets_path %> +
diff --git a/app/views/tsueets/show.html.erb b/app/views/tsueets/show.html.erb new file mode 100644 index 0000000..8326295 --- /dev/null +++ b/app/views/tsueets/show.html.erb @@ -0,0 +1,12 @@ +<%= turbo_stream_from @tsueet %> + +

<%= notice %>

+ +<%= render @tsueet %> + +
+ <%= link_to "Edit this tsueet", edit_tsueet_path(@tsueet) %> | + <%= link_to "Back to tsueets", tsueets_path %> + + <%= button_to "Destroy this tsueet", @tsueet, method: :delete %> +
diff --git a/config/routes.rb b/config/routes.rb index 48254e8..9b2e9ca 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,14 +1,5 @@ Rails.application.routes.draw do - # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html + resources :tsueets - # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. - # Can be used by load balancers and uptime monitors to verify that the app is live. - get "up" => "rails/health#show", as: :rails_health_check - - # Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb) - # get "manifest" => "rails/pwa#manifest", as: :pwa_manifest - # get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker - - # Defines the root path route ("/") - # root "posts#index" + root 'tsueets#index' end diff --git a/db/migrate/20250723100548_create_tsueets.rb b/db/migrate/20250723100548_create_tsueets.rb new file mode 100644 index 0000000..c4197fa --- /dev/null +++ b/db/migrate/20250723100548_create_tsueets.rb @@ -0,0 +1,9 @@ +class CreateTsueets < ActiveRecord::Migration[8.0] + def change + create_table :tsueets do |t| + t.text :content + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..d202d5a --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,19 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[8.0].define(version: 2025_07_23_100548) do + create_table "tsueets", force: :cascade do |t| + t.text "content" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end +end