这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions app/controllers/tsueets_controller.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions app/models/tsueet.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

<%= yield :head %>

<meta name="turbo-refresh-method" content="morph">
<meta name="turbo-refresh-scroll" content="preserve">

<%# 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) %>

Expand All @@ -23,6 +26,9 @@
</head>

<body>
<%= yield %>
<div class="p-3">
<h1>Tsuitter</h1>
<%= yield %>
</div>
</body>
</html>
21 changes: 21 additions & 0 deletions app/views/tsueets/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%= form_with(model: tsueet, data: { turbo_frame: '_top' }) do |form| %>
<% if tsueet.errors.any? %>
<div style="color: red">
<h2><%= pluralize(tsueet.errors.count, "error") %> prohibited this tsueet from being saved:</h2>

<ul>
<% tsueet.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="form-group mb-2">
<%= form.textarea :content, class: "form-control" %>
</div>

<div>
<%= form.submit class: "btn btn-primary" %>
</div>
<% end %>
5 changes: 5 additions & 0 deletions app/views/tsueets/_tsueet.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div id="<%= dom_id tsueet %>" class="card">
<div class="card-body">
<%= tsueet.content %>
</div>
</div>
12 changes: 12 additions & 0 deletions app/views/tsueets/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% content_for :title, "Editing tsueet" %>

<h1>Editing tsueet</h1>

<%= render "form", tsueet: @tsueet %>

<br>

<div>
<%= link_to "Show this tsueet", @tsueet %> |
<%= link_to "Back to tsueets", tsueets_path %>
</div>
14 changes: 14 additions & 0 deletions app/views/tsueets/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% content_for :title, "Tsueets" %>
<%= turbo_stream_from 'tsueets' %>

<div class="mb-4">
<%= render 'form', tsueet: @tsueet %>
</div>

<div id="tsueets">
<% @tsueets.each do |tsueet| %>
<div class="mb-4">
<%= render tsueet %>
</div>
<% end %>
</div>
13 changes: 13 additions & 0 deletions app/views/tsueets/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<% content_for :title, "New tsueet" %>

<h1>New tsueet</h1>

<%= turbo_frame_tag 'new_tsueet' do %>
<%= render "form", tsueet: @tsueet %>
<% end %>

<br>

<div>
<%= link_to "Back to tsueets", tsueets_path %>
</div>
12 changes: 12 additions & 0 deletions app/views/tsueets/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= turbo_stream_from @tsueet %>

<p style="color: green"><%= notice %></p>

<%= render @tsueet %>

<div>
<%= link_to "Edit this tsueet", edit_tsueet_path(@tsueet) %> |
<%= link_to "Back to tsueets", tsueets_path %>

<%= button_to "Destroy this tsueet", @tsueet, method: :delete %>
</div>
13 changes: 2 additions & 11 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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'

Check failure on line 4 in config/routes.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
end
9 changes: 9 additions & 0 deletions db/migrate/20250723100548_create_tsueets.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading