Skip to content

Commit 0ea4386

Browse files
committed
Multi-format responses
1 parent bff97f7 commit 0ea4386

File tree

7 files changed

+123
-21
lines changed

7 files changed

+123
-21
lines changed

app/assets/stylesheets/application.css

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,101 @@
88
*
99
* Consider organizing styles into separate files for maintainability.
1010
*/
11+
12+
body {
13+
font-family: Arial, sans-serif;
14+
background-color: #f7f9fc;
15+
margin: 0;
16+
padding: 0;
17+
color: #333;
18+
display: flex;
19+
justify-content: center;
20+
}
21+
22+
.news-feed {
23+
max-width: 1200px;
24+
width: 100%;
25+
display: grid;
26+
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
27+
gap: 20px;
28+
padding: 20px;
29+
}
30+
31+
.news-card {
32+
position: relative;
33+
overflow: hidden;
34+
border-radius: 10px;
35+
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
36+
transition: transform 0.3s ease;
37+
}
38+
39+
.news-card:hover {
40+
transform: translateY(-5px);
41+
}
42+
43+
.news-card img {
44+
width: 100%;
45+
height: 200px;
46+
object-fit: cover;
47+
display: block;
48+
}
49+
50+
.news-title {
51+
position: absolute;
52+
bottom: 0;
53+
left: 0;
54+
width: 100%;
55+
background: linear-gradient(transparent, rgba(0,0,0,0.7));
56+
color: white;
57+
padding: 15px;
58+
font-size: 1.2rem;
59+
font-weight: bold;
60+
box-sizing: border-box;
61+
}
62+
63+
a {
64+
text-decoration: none;
65+
color: inherit;
66+
}
67+
68+
.article-container {
69+
max-width: 800px;
70+
width: 100%;
71+
margin-top: 20px;
72+
box-shadow: 0 4px 10px rgba(0,0,0,0.1);
73+
border-radius: 10px;
74+
overflow: hidden;
75+
background-color: #fff;
76+
}
77+
78+
.article-image {
79+
position: relative;
80+
width: 100%;
81+
height: 400px;
82+
overflow: hidden;
83+
}
84+
85+
.article-image img {
86+
width: 100%;
87+
height: 100%;
88+
object-fit: cover;
89+
}
90+
91+
.article-title {
92+
position: absolute;
93+
bottom: 0;
94+
left: 0;
95+
width: 100%;
96+
padding: 20px;
97+
background: linear-gradient(transparent, rgba(0,0,0,0.8));
98+
color: white;
99+
font-size: 2rem;
100+
font-weight: bold;
101+
box-sizing: border-box;
102+
}
103+
104+
.article-body {
105+
padding: 25px;
106+
line-height: 1.6;
107+
font-size: 1.1rem;
108+
}

app/controllers/news_controller.rb

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
11
class NewsController < ApplicationController
2-
Mime::Type.register "application/swiftui", :swiftui
3-
42
def index
53
@news = News.all
64

75
respond_to do |format|
8-
format.swiftui do
9-
render template: "news/index"
10-
end
6+
format.html
7+
format.swiftui
118
end
129
end
1310

1411
def show
1512
@article = News.find(params[:id])
1613

1714
respond_to do |format|
18-
format.swiftui do
19-
render template: "news/show"
20-
end
21-
end
22-
end
23-
24-
private
25-
26-
def check_format
27-
if request.headers["Accept"]&.include?("application/swiftui")
28-
request.format = :swiftui
15+
format.html
16+
format.swiftui
2917
end
3018
end
3119
end

app/views/news/index.html.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class="news-feed">
2+
<% @news.each do |article| %>
3+
<a href="/news/<%= article.id %>">
4+
<div class="news-card">
5+
<img src="<%= article.image_url %>" alt="<%= article.title %>">
6+
<div class="news-title"><%= article.title %></div>
7+
</div>
8+
</a>
9+
<% end %>
10+
</div>

app/views/news/show.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div class="article-container">
2+
<div class="article-image">
3+
<img src="<%= @article.image_url %>" alt="<%= @article.title %>">
4+
<div class="article-title"><%= @article.title %></div>
5+
</div>
6+
<div class="article-body">
7+
<%= @article.body.html_safe %>
8+
</div>
9+
</div>

config/initializers/mime_types.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Mime::Type.register "application/swiftui", :swiftui

config/initializers/swiftui_template_handler.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ActionView::Template.register_template_handler :swiftui, ActionView::Template.registered_template_handler(:erb)

0 commit comments

Comments
 (0)