質問編集履歴
1
posts_controller.rbのコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -25,3 +25,177 @@
|
|
25
25
|
|
26
26
|
|
27
27
|
どうかご協力よろしくお願いします。![イメージ説明](afc346dbdef0844625ce1666a0b129ba.png)
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
ーーーーーーーーーーーー↓追加部分
|
32
|
+
|
33
|
+
githubのアドレスも載せます
|
34
|
+
|
35
|
+
https://github.com/tuna114sakana/mylibrary
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
```Ruby
|
40
|
+
|
41
|
+
class PostsController < ApplicationController
|
42
|
+
|
43
|
+
before_action :authenticate_user
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
def index
|
48
|
+
|
49
|
+
@posts=Post.all
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
def new
|
56
|
+
|
57
|
+
@post=Post.new
|
58
|
+
|
59
|
+
@category=Category.find_by(id: params[:id])
|
60
|
+
|
61
|
+
@classification=Classification.new
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
def create
|
68
|
+
|
69
|
+
@post=Post.new(
|
70
|
+
|
71
|
+
name: params[:name],
|
72
|
+
|
73
|
+
content: params[:content],
|
74
|
+
|
75
|
+
user_id: @current_user.id
|
76
|
+
|
77
|
+
)
|
78
|
+
|
79
|
+
@post.save
|
80
|
+
|
81
|
+
@category=Category.find_by(id: params[:category])
|
82
|
+
|
83
|
+
@classification=Classification.new(
|
84
|
+
|
85
|
+
category_id: @category.id,
|
86
|
+
|
87
|
+
post_id: @post.id
|
88
|
+
|
89
|
+
)
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
if @classification.save
|
94
|
+
|
95
|
+
redirect_to("/posts/index")
|
96
|
+
|
97
|
+
else
|
98
|
+
|
99
|
+
@erorr_message="全ての項目をうめてください"
|
100
|
+
|
101
|
+
render("posts/new")
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
if params[:image]
|
106
|
+
|
107
|
+
@post.image_name = "#{@post.id}.jpg"
|
108
|
+
|
109
|
+
image = params[:image]
|
110
|
+
|
111
|
+
File.binwrite("/home/vagrant/mylibrary/myapp/public/post_images/#{@post.image_name}",image.read)
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
def show
|
120
|
+
|
121
|
+
@post=Post.find_by(id: params[:id])
|
122
|
+
|
123
|
+
@user=@post.user
|
124
|
+
|
125
|
+
@category=Post.find_by(id: params[:id]).categories
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
def destroy
|
132
|
+
|
133
|
+
@post=Post.find_by(id: params[:id])
|
134
|
+
|
135
|
+
@post.destroy
|
136
|
+
|
137
|
+
redirect_to("/posts/index")
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
def edit
|
144
|
+
|
145
|
+
@post=Post.find_by(id: params[:id])
|
146
|
+
|
147
|
+
@category=Post.find_by(id: params[:id]).categories
|
148
|
+
|
149
|
+
@classification=Classification.new
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
def update
|
156
|
+
|
157
|
+
@post=Post.find_by(id: params[:id])
|
158
|
+
|
159
|
+
@post.content=params[:content]
|
160
|
+
|
161
|
+
@post.save
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
@category=Category.find_by(id: params[:add_category])
|
166
|
+
|
167
|
+
@classification=Classification.new(
|
168
|
+
|
169
|
+
category_id: @category.id,
|
170
|
+
|
171
|
+
post_id: @post.id
|
172
|
+
|
173
|
+
)
|
174
|
+
|
175
|
+
if @classification.save
|
176
|
+
|
177
|
+
redirect_to("posts/index")
|
178
|
+
|
179
|
+
else
|
180
|
+
|
181
|
+
render("posts/edit")
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
def category
|
188
|
+
|
189
|
+
@category=Category.find_by(name: params[:name])
|
190
|
+
|
191
|
+
@post=Category.find_by(id: @category.id).posts
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
```
|