質問編集履歴
5
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -165,3 +165,103 @@
|
|
165
165
|
</div>
|
166
166
|
|
167
167
|
```
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
```ここに言語を入力
|
172
|
+
|
173
|
+
class PostsController < ApplicationController
|
174
|
+
|
175
|
+
before_action :authenticate_user
|
176
|
+
|
177
|
+
bedore_action :ensure_correct_user, only: [:edit, :update, :destroy]
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
def index
|
182
|
+
|
183
|
+
@posts = Post.all.order(created_at: :desc)
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
def show
|
190
|
+
|
191
|
+
@post = Post.find_by(id: params[:id])
|
192
|
+
|
193
|
+
@user = @post.user
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
def new
|
200
|
+
|
201
|
+
@post = Post.new
|
202
|
+
|
203
|
+
end
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
def create
|
208
|
+
|
209
|
+
post = Post.new(
|
210
|
+
|
211
|
+
post_params,
|
212
|
+
|
213
|
+
user_id: @current_user.id
|
214
|
+
|
215
|
+
)
|
216
|
+
|
217
|
+
post.save
|
218
|
+
|
219
|
+
redirect_to("/")
|
220
|
+
|
221
|
+
end
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
def destroy
|
226
|
+
|
227
|
+
@post = Post.find_by(id: params[:id])
|
228
|
+
|
229
|
+
@post.destroy
|
230
|
+
|
231
|
+
flash[:notice] = "削除...しちゃったよ"
|
232
|
+
|
233
|
+
redirect_to("/")
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
def ensure_correct_user
|
240
|
+
|
241
|
+
@post = Post.find_by(id: params[:id])
|
242
|
+
|
243
|
+
if @post.user_id != @current_user.id
|
244
|
+
|
245
|
+
flash[:notice] = "権限がありません"
|
246
|
+
|
247
|
+
redirect_to("/")
|
248
|
+
|
249
|
+
end
|
250
|
+
|
251
|
+
end
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
private
|
256
|
+
|
257
|
+
def post_params
|
258
|
+
|
259
|
+
params.require(:post).permit(:content, :image)
|
260
|
+
|
261
|
+
end
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
```
|
4
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -67,3 +67,101 @@
|
|
67
67
|
end
|
68
68
|
|
69
69
|
```
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
```route.rb
|
74
|
+
|
75
|
+
Rails.application.routes.draw do
|
76
|
+
|
77
|
+
get "login" => "users#login_form"
|
78
|
+
|
79
|
+
post "login" => "users#login"
|
80
|
+
|
81
|
+
post "logout" => "users#logout"
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
post "users/:id/update" => "users#update"
|
86
|
+
|
87
|
+
get "users/:id/edit" => "users#edit"
|
88
|
+
|
89
|
+
post "users/create" => "users#create"
|
90
|
+
|
91
|
+
get "signup" => "users#new"
|
92
|
+
|
93
|
+
get 'users/index' => "users#index"
|
94
|
+
|
95
|
+
get "users/:id" => "users#show"
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
get "/" => "posts#index"
|
100
|
+
|
101
|
+
get "posts/new" => "posts#new"
|
102
|
+
|
103
|
+
post "posts/create" => "posts#create"
|
104
|
+
|
105
|
+
get "posts/:id" => "posts#show"
|
106
|
+
|
107
|
+
post "posts/:id/destroy" => "posts#destroy"
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
```new.html.erb
|
118
|
+
|
119
|
+
<% @post = Post.new unless @post %>
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
<div class="main posts-new">
|
124
|
+
|
125
|
+
<div class="container">
|
126
|
+
|
127
|
+
<h1 class="form-heading">投稿する</h1>
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
<div class="box2">
|
132
|
+
|
133
|
+
<p>投稿本文の内容は、140文字までです。<br>本文が空の状態での投稿はできません。</p>
|
134
|
+
|
135
|
+
</div>
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
<%= form_for @post,:url => {:action => :create} do |f| %>
|
140
|
+
|
141
|
+
<div class="form">
|
142
|
+
|
143
|
+
<div class="form-body">
|
144
|
+
|
145
|
+
<p>募集内容(最大140文字)</p>
|
146
|
+
|
147
|
+
<%= f.text_area :content, class: "js-text" %>
|
148
|
+
|
149
|
+
<p class="js-text-count"></p>
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
<%= f.file_field :image %>
|
154
|
+
|
155
|
+
<%= f.submit "投稿する" %>
|
156
|
+
|
157
|
+
</div>
|
158
|
+
|
159
|
+
</div>
|
160
|
+
|
161
|
+
<% end %>
|
162
|
+
|
163
|
+
</div>
|
164
|
+
|
165
|
+
</div>
|
166
|
+
|
167
|
+
```
|
3
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,4 @@
|
|
1
|
-
```ここに言語を入力
|
2
|
-
|
3
|
-
コード
|
4
|
-
|
5
|
-
|
1
|
+
railsでコンテンツ投稿機能を作成しており、画像投稿にcarrierwaveを使用しているのですが、一つ問題が発生してしまいました。
|
6
2
|
|
7
3
|
それは、投稿する際に、createアクションで、[The action 'create' could not be found for PostsController]
|
8
4
|
|
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
+
```ここに言語を入力
|
2
|
+
|
3
|
+
コード
|
4
|
+
|
1
|
-
railsでコンテンツ投稿機能を作成しており、画像投稿にcarrierwaveを使用しているのですが、一つ問題が発生してしまいました。
|
5
|
+
```railsでコンテンツ投稿機能を作成しており、画像投稿にcarrierwaveを使用しているのですが、一つ問題が発生してしまいました。
|
2
6
|
|
3
7
|
それは、投稿する際に、createアクションで、[The action 'create' could not be found for PostsController]
|
4
8
|
|
@@ -44,9 +48,7 @@
|
|
44
48
|
|
45
49
|
|
46
50
|
|
47
|
-
|
51
|
+
```post.rb
|
48
|
-
|
49
|
-
|
50
52
|
|
51
53
|
class Post < ApplicationRecord
|
52
54
|
|
1
post.rbファイル追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -41,3 +41,31 @@
|
|
41
41
|
```
|
42
42
|
|
43
43
|
user_idには@current_user.idを設定したいのですが、その場合のpost_paramsの処理をどうすれば良いかがわからず、困っています。ご教授願います。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
{post.rb}
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
class Post < ApplicationRecord
|
52
|
+
|
53
|
+
mount_uploader :image, ImageUploader
|
54
|
+
|
55
|
+
validates :content, {presence: true, length: {maximum: 140}}
|
56
|
+
|
57
|
+
validates :user_id, {presence: true}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
def user
|
62
|
+
|
63
|
+
return User.find_by(id: self.user_id)
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
```
|