Skip to content
This repository was archived by the owner on Mar 9, 2025. It is now read-only.

Commit d013fa3

Browse files
committed
Reproduction for hotwired/turbo-rails#697
1 parent a74afdd commit d013fa3

File tree

9 files changed

+123
-7
lines changed

9 files changed

+123
-7
lines changed

app/controllers/pages_controller.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class PagesController < ApplicationController
2+
before_action :set_page, only: %i[ show ]
3+
4+
# GET /pages or /pages.json
5+
def index
6+
@pages = Page.all
7+
end
8+
9+
# GET /pages/1 or /pages/1.json
10+
def show
11+
end
12+
13+
private
14+
15+
def set_page
16+
@page = Page.find_by(slug: params[:slug])
17+
end
18+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
export default class extends Controller {
4+
initialize() {
5+
super.initialize()
6+
7+
this.observer = new MutationObserver(this.onChange);
8+
}
9+
10+
connect() {
11+
this.observer.observe(document.head, { childList: true })
12+
this.element.textContent = this.metaTagsContent;
13+
}
14+
15+
disconnect() {
16+
delete this.observer;
17+
}
18+
19+
onChange = () => {
20+
this.element.textContent = this.metaTagsContent;
21+
}
22+
23+
get metaTagsContent() {
24+
return Array.from(document.head.querySelectorAll("meta")).reduce(
25+
(result, el) => result + "\n" + el.outerHTML,
26+
"")
27+
}
28+
}

app/models/page.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Page < ApplicationRecord
2+
end

app/views/pages/index.html.erb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<% content_for :title, "Pages" %>
2+
3+
<article>
4+
<h1>Current Page</h1>
5+
<turbo-frame id="page" data-turbo-action="advance"></turbo-frame>
6+
</article>
7+
8+
<nav>
9+
<h2>Navigation</h2>
10+
<ul>
11+
<% @pages.each do |page| %>
12+
<li><%= link_to page.title, page_path(page.slug), data: { turbo_frame: "page" } %></li>
13+
<% end %>
14+
</ul>
15+
</nav>
16+
17+
<article>
18+
<h2>Meta content</h2>
19+
<pre data-controller="meta">
20+
</pre>
21+
</article>

app/views/pages/show.html.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<% content_for :title, @page.title %>
2+
<% content_for(:head) do %>
3+
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
4+
<%= javascript_importmap_tags %>
5+
<% end %>
6+
7+
<%= link_to("Home", root_path) %>
8+
9+
<div>
10+
<%= turbo_frame_tag("page") do %>
11+
<%= @page.body %>
12+
<% end %>
13+
</div>

config/routes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
1010
# get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
1111

12+
resources :pages, param: :slug
13+
1214
# Defines the root path route ("/")
13-
# root "posts#index"
15+
root "pages#index"
1416
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreatePages < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :pages do |t|
4+
t.string :slug
5+
t.string :title
6+
t.string :body
7+
8+
t.timestamps
9+
end
10+
end
11+
end

db/schema.rb

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/seeds.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# This file should ensure the existence of records required to run the application in every environment (production,
22
# development, test). The code here should be idempotent so that it can be executed at any point in every environment.
33
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
4-
#
5-
# Example:
6-
#
7-
# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
8-
# MovieGenre.find_or_create_by!(name: genre_name)
9-
# end
4+
5+
["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
6+
Page.find_or_create_by!(slug: genre_name.parameterize) do |page|
7+
page.update(title: genre_name, body: genre_name)
8+
end
9+
end

0 commit comments

Comments
 (0)