質問編集履歴
1
noteのmodelの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -133,3 +133,151 @@
|
|
133
133
|
end
|
134
134
|
|
135
135
|
のように記載しています。
|
136
|
+
|
137
|
+
以下、追記です。
|
138
|
+
|
139
|
+
notes_controller.rbは
|
140
|
+
|
141
|
+
class NotesController < ApplicationController
|
142
|
+
|
143
|
+
before_action :authenticate_user!
|
144
|
+
|
145
|
+
before_action :correct_user, only: [:edit, :update]
|
146
|
+
|
147
|
+
before_action :set_note,only:[:show,:edit,:update,:destroy]
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
def create
|
152
|
+
|
153
|
+
@note = Note.new(note_params)
|
154
|
+
|
155
|
+
if @note.save
|
156
|
+
|
157
|
+
redirect_to @note,notice:'投稿が保存されました'
|
158
|
+
|
159
|
+
else
|
160
|
+
|
161
|
+
@notes = Note.all.order(created_at: :desc)
|
162
|
+
|
163
|
+
render 'home/top'
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
def show
|
174
|
+
|
175
|
+
@notes = Note.find(params[:id])
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
def edit
|
182
|
+
|
183
|
+
@notes = Note.find(params[:id])
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
def update
|
190
|
+
|
191
|
+
if @note.update(note_params)
|
192
|
+
|
193
|
+
redirect_to @note, notice: "投稿が更新されました"
|
194
|
+
|
195
|
+
else
|
196
|
+
|
197
|
+
render :edit
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
def destroy
|
206
|
+
|
207
|
+
@note.destroy
|
208
|
+
|
209
|
+
redirect_to notes_path
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
def liking_users
|
216
|
+
|
217
|
+
@users = @note.liking_users
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
private
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
def set_note
|
228
|
+
|
229
|
+
@note = Note.find(params[:id])
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
def note_params
|
236
|
+
|
237
|
+
params.require(:note).permit(:content)
|
238
|
+
|
239
|
+
end
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
def correct_user
|
244
|
+
|
245
|
+
note = Note.find(params[:id])
|
246
|
+
|
247
|
+
# noteを投稿したユーザーを取得し、current_user?メソッドの引数に渡してください
|
248
|
+
|
249
|
+
if !current_user?(note.user)
|
250
|
+
|
251
|
+
redirect_to root_path, alert: '許可されていないページです'
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
end
|
260
|
+
|
261
|
+
のようになっていて、 noteのmodelのnote.rbは
|
262
|
+
|
263
|
+
class Note < ActiveRecord::Base
|
264
|
+
|
265
|
+
belongs_to :user
|
266
|
+
|
267
|
+
has_many :likes, dependent: :destroy
|
268
|
+
|
269
|
+
has_many :liking_users, through: :likes, source: :user
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
validates :content, presence: true, length: { maximum: 140 }
|
276
|
+
|
277
|
+
validates :user_id, presence: true
|
278
|
+
|
279
|
+
end
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
のようになっています。 よろしくお願い申し上げます。
|