質問編集履歴

3

コード修正

2020/08/29 16:27

投稿

KURUMAEBI
KURUMAEBI

スコア2

test CHANGED
File without changes
test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
  def destroy
36
36
 
37
- BlogComment.find_by(id: params[:id]).destroy
37
+ BlogComment.find_by(id: params[:id], blog_id: params[:blog.id]).destroy
38
38
 
39
39
  redirect_to public_blog_path(blog)
40
40
 

2

コード修正

2020/08/29 16:27

投稿

KURUMAEBI
KURUMAEBI

スコア2

test CHANGED
File without changes
test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
  def destroy
36
36
 
37
- BlogComment.find_by(id: params[:id], blog_id: params[:blog_id]).destroy
37
+ BlogComment.find_by(id: params[:id]).destroy
38
38
 
39
39
  redirect_to public_blog_path(blog)
40
40
 
@@ -58,6 +58,122 @@
58
58
 
59
59
 
60
60
 
61
+ ```
62
+
63
+ ### blogs_controller.rb
64
+
65
+
66
+
67
+ class Public::BlogsController < ApplicationController
68
+
69
+ before_action :authenticate_user!, except: [:index]
70
+
71
+ def index
72
+
73
+ @blogs = Blog.search(params[:search])
74
+
75
+ @blog = Blog.new
76
+
77
+ end
78
+
79
+
80
+
81
+ def create
82
+
83
+ @blog = Blog.new(blog_params)
84
+
85
+ @blogs = Blog.all
86
+
87
+ @blog.user_id = current_user.id
88
+
89
+ if @blog.save!
90
+
91
+ redirect_to public_blogs_path(@blog), notice: 'Create succsessfuly'
92
+
93
+ else
94
+
95
+ render :create
96
+
97
+ end
98
+
99
+ end
100
+
101
+
102
+
103
+ def show
104
+
105
+ @blog = Blog.find(params[:id])
106
+
107
+ @blog_comments = @blog.blog_comments
108
+
109
+ @blog_comment = current_user.blog_comments.new
110
+
111
+ end
112
+
113
+
114
+
115
+ def edit
116
+
117
+ @blog = Blog.find(params[:id])
118
+
119
+ if @blog.user != current_user
120
+
121
+ redirect_to public_blogs_path, alert: 'Invalid Access'
122
+
123
+ end
124
+
125
+ end
126
+
127
+
128
+
129
+ def update
130
+
131
+ @blog = Blog.find(params[:id])
132
+
133
+ if @blog.update!(blog_params)
134
+
135
+ redirect_to public_blogs_path(@blog.id), notice: 'Update succsessfuly'
136
+
137
+ else
138
+
139
+ render :edit
140
+
141
+ end
142
+
143
+ end
144
+
145
+
146
+
147
+ def destroy
148
+
149
+ blog = Blog.find(params[:id])
150
+
151
+ blog.destroy
152
+
153
+ redirect_to public_blogs_path, notice: 'Deleted succsessfuly'
154
+
155
+ end
156
+
157
+
158
+
159
+ private
160
+
161
+
162
+
163
+ def blog_params
164
+
165
+ params.require(:blog).permit(:title, :body, :blog_image_id)
166
+
167
+ end
168
+
169
+ end
170
+
171
+
172
+
173
+ ```
174
+
175
+
176
+
61
177
  ```/public/blogs/show.html.erb
62
178
 
63
179
  ### /public/blogs/show.html.erb
@@ -102,7 +218,7 @@
102
218
 
103
219
  <div>
104
220
 
105
- <%= link_to "削除", public_blog_blog_comment_path(blog_comment.comment), method: :delete, class: "btn btn-danger pull-right" %>
221
+ <%= link_to "削除", public_blog_blog_comment_path(blog_comment), method: :delete, class: "btn btn-danger pull-right" %>
106
222
 
107
223
  </div>
108
224
 

1

タグ変更、補足説明、説明追加

2020/08/29 13:36

投稿

KURUMAEBI
KURUMAEBI

スコア2

test CHANGED
File without changes
test CHANGED
@@ -9,6 +9,10 @@
9
9
 
10
10
 
11
11
  ```/controller/blog_comments_controller.rb
12
+
13
+ ### /controller/blog_comments_controller.rb
14
+
15
+
12
16
 
13
17
  class Public::BlogCommentsController < ApplicationController
14
18
 
@@ -55,6 +59,10 @@
55
59
 
56
60
 
57
61
  ```/public/blogs/show.html.erb
62
+
63
+ ### /public/blogs/show.html.erb
64
+
65
+
58
66
 
59
67
  <% @blog.blog_comments.each do |blog_comment| %>
60
68
 
@@ -118,6 +126,10 @@
118
126
 
119
127
  ```/migrate/create_blog_comments.rb
120
128
 
129
+ ### /migrate/create_blog_comments.rb
130
+
131
+
132
+
121
133
  class CreateBlogComments < ActiveRecord::Migration[5.2]
122
134
 
123
135
  def change