質問編集履歴

1

内容の変更

2020/09/19 07:00

投稿

kei__3
kei__3

スコア7

test CHANGED
File without changes
test CHANGED
@@ -26,162 +26,162 @@
26
26
 
27
27
  <div class="container">
28
28
 
29
- <div class="row">
29
+ <div class="row">
30
-
30
+
31
- <h1>タイムライン</h1>
31
+ <h1>タイムライン</h1>
32
-
32
+
33
- <%= render 'shared/feed' %>
33
+ <%= render 'shared/feed' %>
34
+
35
+ </div>
36
+
37
+ </div>
38
+
39
+
40
+
41
+ ===================================shared/_feed.html.erb===================================
42
+
43
+
44
+
45
+ <% if @feed_items.any? %>
46
+
47
+ <%= render @feed_items %>
48
+
49
+ <%= will_paginate @feed_items %>
50
+
51
+ <% end %>
52
+
53
+
54
+
55
+ ===================================posts_controller.rb===================================
56
+
57
+
58
+
59
+ class PostsController < ApplicationController
60
+
61
+ before_action :logged_in_user, only: [:new, :index, :create, :destroy]
62
+
63
+ before_action :correct_user, only: :destroy
64
+
65
+
66
+
67
+ def new
68
+
69
+ @post = current_user.posts.build if logged_in?
70
+
71
+ end
72
+
73
+
74
+
75
+ def index
76
+
77
+ @feed_items = current_user.feed.paginate(page: params[:page]) if logged_in?
78
+
79
+ end
80
+
81
+
82
+
83
+ def show
84
+
85
+ end
86
+
87
+
88
+
89
+ def create
90
+
91
+ @post = current_user.posts.build(post_params)
92
+
93
+ @post.image.attach(params[:post][:image])
94
+
95
+ if @post.save
96
+
97
+ flash[:success] = "投稿しました"
98
+
99
+ redirect_to posts_url
100
+
101
+ else
102
+
103
+ @feed_items = current_user.posts.build(post_params)
104
+
105
+ render 'posts/new'
106
+
107
+ end
108
+
109
+ end
110
+
111
+
112
+
113
+ def destroy
114
+
115
+ @post.destroy
116
+
117
+ flash[:success] ="投稿を削除しました"
118
+
119
+ redirect_to request.referrer || posts_url
120
+
121
+ end
122
+
123
+
124
+
125
+ private
126
+
127
+
128
+
129
+ def post_params
130
+
131
+ params.require(:post).permit(:content, :image)
132
+
133
+ end
134
+
135
+
136
+
137
+ def correct_user
138
+
139
+ @post = current_user.posts.find_by(id: params[:id])
140
+
141
+ redirect_to root_url if @post.nil?
142
+
143
+ end
144
+
145
+ end
146
+
147
+
148
+
149
+ ======================================_post.html.erb======================================
150
+
151
+
152
+
153
+ <div class="col-md-4">
154
+
155
+ <div class="timeline">
156
+
157
+ <div class="timeline-layout">
158
+
159
+ <%= link_to image_tag(post.display_image), post_path(post.id) %>
34
160
 
35
161
  </div>
36
162
 
163
+ <div class="timeline-icon">
164
+
165
+ <%= link_to gravatar_for(post.user, size:50), post.user %>
166
+
167
+ </div>
168
+
169
+ <div class="timeline-name">
170
+
171
+ <%= link_to post.user.name, post.user %>
172
+
173
+ </div>
174
+
175
+ <% if current_user?(post.user) %>
176
+
177
+ <%#= link_to "削除",post, method: :delete,
178
+
179
+ data: {confirm: "削除しますか?" } %>
180
+
181
+ <% end %>
182
+
37
183
  </div>
38
184
 
39
-
40
-
41
- ===================================shared/_feed.html.erb===================================
42
-
43
-
44
-
45
- <% if @feed_items.any? %>
46
-
47
- <%= render @feed_items %>
48
-
49
- <%= will_paginate @feed_items %>
50
-
51
- <% end %>
52
-
53
-
54
-
55
- ===================================posts_controller.rb===================================
56
-
57
-
58
-
59
- class PostsController < ApplicationController
60
-
61
- before_action :logged_in_user, only: [:new, :index, :create, :destroy]
62
-
63
- before_action :correct_user, only: :destroy
64
-
65
-
66
-
67
- def new
68
-
69
- @post = current_user.posts.build if logged_in?
70
-
71
- end
72
-
73
-
74
-
75
- def index
76
-
77
- @feed_items = current_user.feed.paginate(page: params[:page]) if logged_in?
78
-
79
- end
80
-
81
-
82
-
83
- def show
84
-
85
- end
86
-
87
-
88
-
89
- def create
90
-
91
- @post = current_user.posts.build(post_params)
92
-
93
- @post.image.attach(params[:post][:image])
94
-
95
- if @post.save
96
-
97
- flash[:success] = "投稿しました"
98
-
99
- redirect_to posts_url
100
-
101
- else
102
-
103
- @feed_items = current_user.posts.build(post_params)
104
-
105
- render 'posts/new'
106
-
107
- end
108
-
109
- end
110
-
111
-
112
-
113
- def destroy
114
-
115
- @post.destroy
116
-
117
- flash[:success] ="投稿を削除しました"
118
-
119
- redirect_to request.referrer || posts_url
120
-
121
- end
122
-
123
-
124
-
125
- private
126
-
127
-
128
-
129
- def post_params
130
-
131
- params.require(:post).permit(:content, :image)
132
-
133
- end
134
-
135
-
136
-
137
- def correct_user
138
-
139
- @post = current_user.posts.find_by(id: params[:id])
140
-
141
- redirect_to root_url if @post.nil?
142
-
143
- end
144
-
145
- end
146
-
147
-
148
-
149
- ======================================_post.html.erb======================================
150
-
151
-
152
-
153
- <div class="col-md-4">
154
-
155
- <div class="timeline">
156
-
157
- <div class="timeline-layout">
158
-
159
- <%= link_to image_tag(post.display_image), post_path(post.id) %>
160
-
161
- </div>
162
-
163
- <div class="timeline-icon">
164
-
165
- <%= link_to gravatar_for(post.user, size:50), post.user %>
166
-
167
- </div>
168
-
169
- <div class="timeline-name">
170
-
171
- <%= link_to post.user.name, post.user %>
172
-
173
- </div>
174
-
175
- <% if current_user?(post.user) %>
176
-
177
- <%#= link_to "削除",post, method: :delete,
178
-
179
- data: {confirm: "削除しますか?" } %>
180
-
181
- <% end %>
182
-
183
- </div>
184
-
185
185
  </div>
186
186
 
187
187