質問編集履歴

1

必要情報の記載が足りないとのご指摘を受けたので、上から順にcontroller・routes・viewを追加致しました。

2021/02/18 11:40

投稿

pops
pops

スコア0

test CHANGED
File without changes
test CHANGED
@@ -56,6 +56,112 @@
56
56
 
57
57
 
58
58
 
59
+
60
+
61
+ class PostsController < ApplicationController
62
+
63
+ def index
64
+
65
+ @posts = Post.all.order(created_at: :desc)
66
+
67
+ end
68
+
69
+
70
+
71
+ def show
72
+
73
+ @post = Post.find_by(id: params[:id])
74
+
75
+ end
76
+
77
+
78
+
79
+ def new
80
+
81
+ end
82
+
83
+
84
+
85
+ def create
86
+
87
+ @post = Post.new(content: params[:content])
88
+
89
+ @post.save
90
+
91
+ redirect_to("/posts/index")
92
+
93
+ end
94
+
95
+ end
96
+
97
+
98
+
99
+ Rails.application.routes.draw do
100
+
101
+
102
+
103
+ get "posts/index" => "posts#index"
104
+
105
+ get "posts/new" => "posts#new"
106
+
107
+ get "posts/:id" => "posts#show"
108
+
109
+ post "posts/create" => "posts#create"
110
+
111
+
112
+
113
+ get "/" => "home#top"
114
+
115
+ get "about" => "home#about"
116
+
117
+ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
118
+
119
+ end
120
+
121
+
122
+
123
+ <header>
124
+
125
+ <div class="header-logo">
126
+
127
+ <a href="/">TweetApp</a>
128
+
129
+ </div>
130
+
131
+ <ul class="header-menus">
132
+
133
+ <li>
134
+
135
+ <a href="/about">TweetAppとは</a>
136
+
137
+ </li>
138
+
139
+ </ul>
140
+
141
+ </header>
142
+
143
+
144
+
145
+
146
+
147
+ <div class="main posts-index">
148
+
149
+ <div class="container">
150
+
151
+ <% @posts.each do |post| %>
152
+
153
+ <div class="posts-index-item">
154
+
155
+ <%= link_to(post.content, "/posts/#{post.id}") %>
156
+
157
+ </div>
158
+
159
+ <% end %>
160
+
161
+ </div>
162
+
163
+ </div>
164
+
59
165
  ```
60
166
 
61
167