質問編集履歴
1
controller追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -104,6 +104,120 @@
|
|
104
104
|
|
105
105
|
```ruby
|
106
106
|
|
107
|
+
[articles_controller.rb ]
|
108
|
+
|
109
|
+
class ArticlesController < ApplicationController
|
110
|
+
|
111
|
+
before_action :find_article, only: [:show, :edit, :destroy, :update]
|
112
|
+
|
113
|
+
def index
|
114
|
+
|
115
|
+
@articles = Article.order(created_at: :desc)
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
def show
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
def new
|
128
|
+
|
129
|
+
@article = Article.new
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
def create
|
136
|
+
|
137
|
+
@article = Article.new(params_article)
|
138
|
+
|
139
|
+
byebug
|
140
|
+
|
141
|
+
if @article.save
|
142
|
+
|
143
|
+
redirect_to articles_path, notice: "新規作成できました。"
|
144
|
+
|
145
|
+
else
|
146
|
+
|
147
|
+
render :new, alert: "作成に失敗しました。"
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
def edit
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
def update
|
164
|
+
|
165
|
+
if @article.update(params_article)
|
166
|
+
|
167
|
+
redirect_to articles_path, notice: "更新できました。"
|
168
|
+
|
169
|
+
else
|
170
|
+
|
171
|
+
render :edit, alert: "更新に失敗しました。"
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
def destroy
|
180
|
+
|
181
|
+
if @article.destroy
|
182
|
+
|
183
|
+
redirect_to articles_path, notice: "削除しました。"
|
184
|
+
|
185
|
+
else
|
186
|
+
|
187
|
+
render :show, alert: "削除できませんでした。"
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
private
|
196
|
+
|
197
|
+
def find_article
|
198
|
+
|
199
|
+
@article = Article.find(params[:id])
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
def params_article
|
206
|
+
|
207
|
+
params.require(:article).permit(:title, :content)
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
```
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
```ruby
|
220
|
+
|
107
221
|
[ _form.html.erb ]
|
108
222
|
|
109
223
|
<%= form_with model: article do |f| %>
|