質問編集履歴

1

スキーマ情報、コントローラー情報の追加

2019/04/25 20:03

投稿

ma5toy
ma5toy

スコア15

test CHANGED
File without changes
test CHANGED
@@ -148,6 +148,180 @@
148
148
 
149
149
  ```
150
150
 
151
+ ```
152
+
153
+ スキーマ
154
+
155
+ create_table "articles", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
156
+
157
+ t.text "article_title", null: false
158
+
159
+ t.text "article_content", null: false
160
+
161
+ t.integer "user_id", null: false
162
+
163
+ t.integer "category_id"
164
+
165
+ t.integer "sub_category_id"
166
+
167
+ t.string "article_thumbnail"
168
+
169
+ t.integer "impressions_count", default: 0
170
+
171
+ t.datetime "created_at", null: false
172
+
173
+ t.datetime "updated_at", null: false
174
+
175
+ end
176
+
177
+ ```
178
+
179
+ ```
180
+
181
+ コントローラー
182
+
183
+ class Users::ArticlesController < ApplicationController
184
+
185
+ before_action :require_login
186
+
187
+ before_action :logedin_admin?
188
+
189
+
190
+
191
+ def show
192
+
193
+ @article = Article.find(params[:id])
194
+
195
+ end
196
+
197
+
198
+
199
+ def new
200
+
201
+ @article = Article.new
202
+
203
+ end
204
+
205
+
206
+
207
+ def edit
208
+
209
+ @article = Article.find(params[:id])
210
+
211
+ end
212
+
213
+
214
+
215
+ def create
216
+
217
+ @article = Article.new(article_params)
218
+
219
+ respond_to do |format|
220
+
221
+ begin
222
+
223
+ ActiveRecord::Base.transaction do
224
+
225
+ @article.save!
226
+
227
+ end
228
+
229
+ # 正常処理
230
+
231
+ format.html { redirect_to articles_path }
232
+
233
+ format.json { render :index, location: @article }
234
+
235
+ rescue ActiveRecord::RecordInvalid
236
+
237
+ # バリデーション処理
238
+
239
+ @article.valid?
240
+
241
+ format.html { render :new }
242
+
243
+ format.json { render json: @article.errors }
244
+
245
+ rescue => e
246
+
247
+ Rails.logger.error e
248
+
249
+ Rails.logger.error e.backtrace.join("\n")
250
+
251
+ Raven.capture_exception(e)
252
+
253
+ flash.now[:danger] = "保存に失敗しました"
254
+
255
+ format.html { render :new }
256
+
257
+ end
258
+
259
+ end
260
+
261
+ end
262
+
263
+
264
+
265
+ def update
266
+
267
+ @article = Article.find(params[:id])
268
+
269
+ if @article.update!(article_params)
270
+
271
+ redirect_to root_url
272
+
273
+ else
274
+
275
+ render :edit
276
+
277
+ end
278
+
279
+ end
280
+
281
+
282
+
283
+ def destroy
284
+
285
+ @article = Article.find(params[:id])
286
+
287
+ @article.destroy!
288
+
289
+ redirect_to users_articles_path
290
+
291
+ end
292
+
293
+
294
+
295
+ private
296
+
297
+
298
+
299
+ def article_params
300
+
301
+ params.require(:article).permit(:article_title, :article_content, :article_thumbnail, :article_thumbnail_cache, :category_id, :sub_category_id, {:tag_ids => []}, :user_id)
302
+
303
+ end
304
+
305
+
306
+
307
+ def logedin_admin?
308
+
309
+ redirect_to articles_path unless current_user.admin?
310
+
311
+ end
312
+
313
+
314
+
315
+ def set_user
316
+
317
+
318
+
319
+ end
320
+
321
+ end
322
+
323
+ ```
324
+
151
325
 
152
326
 
153
327
  ### 試したこと