質問するログイン新規登録

質問編集履歴

1

posts_controller.rbのコードの追加

2019/07/25 14:24

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -11,4 +11,91 @@
11
11
  考えられる原因は何でしょうか
12
12
  大変困っています。
13
13
 
14
- どうかご協力よろしくお願いします。![イメージ説明](afc346dbdef0844625ce1666a0b129ba.png)
14
+ どうかご協力よろしくお願いします。![イメージ説明](afc346dbdef0844625ce1666a0b129ba.png)
15
+
16
+ ーーーーーーーーーーーー↓追加部分
17
+ githubのアドレスも載せます
18
+ https://github.com/tuna114sakana/mylibrary
19
+
20
+ ```Ruby
21
+ class PostsController < ApplicationController
22
+ before_action :authenticate_user
23
+
24
+ def index
25
+ @posts=Post.all
26
+ end
27
+
28
+ def new
29
+ @post=Post.new
30
+ @category=Category.find_by(id: params[:id])
31
+ @classification=Classification.new
32
+ end
33
+
34
+ def create
35
+ @post=Post.new(
36
+ name: params[:name],
37
+ content: params[:content],
38
+ user_id: @current_user.id
39
+ )
40
+ @post.save
41
+ @category=Category.find_by(id: params[:category])
42
+ @classification=Classification.new(
43
+ category_id: @category.id,
44
+ post_id: @post.id
45
+ )
46
+
47
+ if @classification.save
48
+ redirect_to("/posts/index")
49
+ else
50
+ @erorr_message="全ての項目をうめてください"
51
+ render("posts/new")
52
+ end
53
+ if params[:image]
54
+ @post.image_name = "#{@post.id}.jpg"
55
+ image = params[:image]
56
+ File.binwrite("/home/vagrant/mylibrary/myapp/public/post_images/#{@post.image_name}",image.read)
57
+ end
58
+ end
59
+
60
+ def show
61
+ @post=Post.find_by(id: params[:id])
62
+ @user=@post.user
63
+ @category=Post.find_by(id: params[:id]).categories
64
+ end
65
+
66
+ def destroy
67
+ @post=Post.find_by(id: params[:id])
68
+ @post.destroy
69
+ redirect_to("/posts/index")
70
+ end
71
+
72
+ def edit
73
+ @post=Post.find_by(id: params[:id])
74
+ @category=Post.find_by(id: params[:id]).categories
75
+ @classification=Classification.new
76
+ end
77
+
78
+ def update
79
+ @post=Post.find_by(id: params[:id])
80
+ @post.content=params[:content]
81
+ @post.save
82
+
83
+ @category=Category.find_by(id: params[:add_category])
84
+ @classification=Classification.new(
85
+ category_id: @category.id,
86
+ post_id: @post.id
87
+ )
88
+ if @classification.save
89
+ redirect_to("posts/index")
90
+ else
91
+ render("posts/edit")
92
+ end
93
+
94
+ def category
95
+ @category=Category.find_by(name: params[:name])
96
+ @post=Category.find_by(id: @category.id).posts
97
+ end
98
+
99
+ end
100
+
101
+ ```