質問編集履歴

1

controllerを全文追記

2021/09/08 01:23

投稿

husqy
husqy

スコア2

test CHANGED
File without changes
test CHANGED
@@ -78,104 +78,190 @@
78
78
 
79
79
 
80
80
 
81
+ class RoadsController < ApplicationController
82
+
83
+ PER_PAGE = 15
84
+
85
+
86
+
87
+ def index
88
+
89
+ @q = Road.ransack(params[:q])
90
+
91
+ @roads = @q.result.includes(:user, :likes).page(params[:page]).per(PER_PAGE).order(:created_at)
92
+
93
+ end
94
+
95
+
96
+
97
+ def show
98
+
99
+ @road = Road.find(params[:id])
100
+
101
+ @comments = @road.comments
102
+
103
+ @comment = Comment.new
104
+
105
+ end
106
+
107
+
108
+
109
+ def new
110
+
111
+ @road = Road.new
112
+
113
+ end
114
+
115
+
116
+
117
+ def create
118
+
119
+ @road = Road.new(road_params)
120
+
121
+ if @road.save
122
+
123
+ flash[:success] = "投稿が完了しました"
124
+
125
+ redirect_to roads_path
126
+
127
+ else
128
+
129
+ flash[:alert] = "投稿に失敗しました"
130
+
131
+ render :new
132
+
133
+ end
134
+
135
+ end
136
+
137
+
138
+
139
+ def edit
140
+
141
+ @road = Road.find(params[:id])
142
+
143
+ end
144
+
145
+
146
+
147
+ def update
148
+
149
+ road = Road.find(params[:id])
150
+
151
+ if params[:road][:road_image_ids]
152
+
153
+ params[:road][:road_image_ids].each do |road_image_id|
154
+
155
+ image = road.road_images.find(road_image_id)
156
+
157
+ image.purge
158
+
159
+ end
160
+
161
+ end
162
+
163
+ if road.update(road_params)
164
+
165
+ flash[:success] = "投稿を編集しました"
166
+
167
+ redirect_to roads_path
168
+
169
+ else
170
+
171
+ flash[:alert] = "更新に失敗しました"
172
+
173
+ render :edit
174
+
175
+ end
176
+
177
+ end
178
+
179
+
180
+
181
+ def destroy
182
+
183
+ Road.find(params[:id]).destroy
184
+
185
+ flash[:success] = "投稿を削除しました"
186
+
187
+ redirect_to roads_path
188
+
189
+ end
190
+
191
+
192
+
193
+ private
194
+
195
+ def road_params
196
+
197
+ params.require(:road).permit(:title, :description, :latitude, :longitude, :content, road_images: []).merge(user_id: current_user.id)
198
+
199
+ end
200
+
201
+ end
202
+
203
+
204
+
205
+ ```
206
+
207
+
208
+
209
+ ```Ruby
210
+
211
+
212
+
213
+ # road.rb
214
+
215
+
216
+
217
+ class Road < ApplicationRecord
218
+
219
+ has_many_attached :road_images
220
+
221
+ has_many :comments, dependent: :destroy
222
+
223
+ has_many :likes, dependent: :destroy
224
+
225
+ has_many :liked_users, through: :likes, source: :user
226
+
227
+ belongs_to :user
228
+
229
+
230
+
231
+ validates :latitude, :longitude, presence: true
232
+
233
+ validates :title, presence: true, length: { maximum: 50 }
234
+
235
+ validates :description, presence: true, length: { maximum: 400 }
236
+
237
+
238
+
239
+ def liked_by?(user)
240
+
241
+ likes.any?{ |like| like.user_id == user.id }
242
+
243
+ end
244
+
245
+ end
246
+
247
+ ```
248
+
249
+
250
+
251
+ ```ERB
252
+
253
+ # edit.html.erb
254
+
255
+
256
+
257
+ <div class="container py-5 mx-auto">
258
+
259
+ <h3 class="text-center">編集</h3>
260
+
261
+
262
+
81
263
  (略)
82
264
 
83
- def update
84
-
85
- road = Road.find(params[:id])
86
-
87
- if params[:road][:road_image_ids]
88
-
89
- params[:road][:road_image_ids].each do |road_image_id|
90
-
91
- image = road.road_images.find(road_image_id)
92
-
93
- image.purge
94
-
95
- end
96
-
97
- end
98
-
99
- if road.update(road_params)
100
-
101
- flash[:success] = "投稿を編集しました"
102
-
103
- redirect_to roads_path
104
-
105
- else
106
-
107
- flash[:alert] = "更新に失敗しました"
108
-
109
- render :edit
110
-
111
- end
112
-
113
- end
114
-
115
- (略)
116
-
117
-
118
-
119
- ```
120
-
121
-
122
-
123
- ```Ruby
124
-
125
-
126
-
127
- # road.rb
128
-
129
-
130
-
131
- class Road < ApplicationRecord
132
-
133
- has_many_attached :road_images
134
-
135
- has_many :comments, dependent: :destroy
136
-
137
- has_many :likes, dependent: :destroy
138
-
139
- has_many :liked_users, through: :likes, source: :user
140
-
141
- belongs_to :user
142
-
143
-
144
-
145
- validates :latitude, :longitude, presence: true
146
-
147
- validates :title, presence: true, length: { maximum: 50 }
148
-
149
- validates :description, presence: true, length: { maximum: 400 }
150
-
151
-
152
-
153
- def liked_by?(user)
154
-
155
- likes.any?{ |like| like.user_id == user.id }
156
-
157
- end
158
-
159
- end
160
-
161
- ```
162
-
163
-
164
-
165
- ```ERB
166
-
167
- # edit.html.erb
168
-
169
-
170
-
171
- <div class="container py-5 mx-auto">
172
-
173
- <h3 class="text-center">編集</h3>
174
-
175
-
176
-
177
- (略)
178
-
179
265
 
180
266
 
181
267
  </div>