質問編集履歴

1

コード追加

2019/11/26 09:33

投稿

MISIAN-MISIAN
MISIAN-MISIAN

スコア73

test CHANGED
File without changes
test CHANGED
@@ -85,3 +85,163 @@
85
85
  .main-photo{style: "background-image: url(#{tweet.image});"}
86
86
 
87
87
  ```
88
+
89
+
90
+
91
+ ```ここに言語を入力
92
+
93
+ ファイル<routes.rb>
94
+
95
+ Rails.application.routes.draw do
96
+
97
+
98
+
99
+ devise_for :users
100
+
101
+ root to: "tweets#index"
102
+
103
+ namespace :tweets do
104
+
105
+ resources :searches, only: :index
106
+
107
+ end
108
+
109
+ resources :tweets do
110
+
111
+ resources :comments, only: [:create, :destroy]
112
+
113
+ end
114
+
115
+ resources :users, only: :show
116
+
117
+
118
+
119
+ end
120
+
121
+ ```
122
+
123
+
124
+
125
+ ```ここに言語を入力
126
+
127
+ ファイル名<tweets.controller.rb>
128
+
129
+ class TweetsController < ApplicationController
130
+
131
+ before_action :set_tweet, only: [:edit, :show]
132
+
133
+ before_action :move_to_index, except: [:index, :show]
134
+
135
+
136
+
137
+ def index
138
+
139
+ @tweets = Tweet.includes(:user).order("created_at DESC").page(params[:page]).per(6)
140
+
141
+ @random = Tweet.order("RAND()").limit(1)
142
+
143
+
144
+
145
+ end
146
+
147
+
148
+
149
+ def new
150
+
151
+ @tweet = Tweet.new
152
+
153
+ end
154
+
155
+
156
+
157
+ def create
158
+
159
+ Tweet.create(tweet_params)
160
+
161
+
162
+
163
+ end
164
+
165
+
166
+
167
+ def destroy
168
+
169
+ tweet = Tweet.find(params[:id])
170
+
171
+ tweet.destroy
172
+
173
+
174
+
175
+ end
176
+
177
+
178
+
179
+ def edit
180
+
181
+
182
+
183
+ end
184
+
185
+
186
+
187
+ def update
188
+
189
+ tweet = Tweet.find(params[:id])
190
+
191
+ tweet.update(tweet_params)
192
+
193
+ end
194
+
195
+
196
+
197
+ def show
198
+
199
+ @comment = Comment.new
200
+
201
+ @comments = @tweet.comments.includes(:user)
202
+
203
+ end
204
+
205
+
206
+
207
+ def search
208
+
209
+ @tweets = Tweet.search(params[:keyword])
210
+
211
+ end
212
+
213
+
214
+
215
+ private
216
+
217
+ def tweet_params
218
+
219
+ params.require(:tweet).permit(:image, :text).merge(user_id: current_user.id)
220
+
221
+ end
222
+
223
+
224
+
225
+ def set_tweet
226
+
227
+ @tweet = Tweet.find(params[:id])
228
+
229
+ end
230
+
231
+
232
+
233
+ def move_to_index
234
+
235
+ redirect_to action: :index unless user_signed_in?
236
+
237
+ end
238
+
239
+
240
+
241
+
242
+
243
+ end
244
+
245
+
246
+
247
+ ```