質問編集履歴
1
内容修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,41 +1,172 @@
|
|
1
1
|
新規投稿画面でpost_pathの未定義エラーが発生します。
|
2
|
-
新規投稿の空投稿に対するエラーメッセージを表
|
2
|
+
新規投稿の空投稿に対するエラーメッセージを表示するために実装しているときに起きている事象です。
|
3
3
|
```
|
4
4
|
undefined method `posts_path' for #<#<Class:0x00007f880a71ddd0>:0x00007f880a727e70>
|
5
5
|
```
|
6
6
|
```new.html.erb
|
7
|
+
<div class="main posts-new">
|
8
|
+
<div class="container">
|
9
|
+
<h1 class="form-heading"style="text-align: center">投稿する</h1>
|
10
|
+
<%= form_with(model: @post, local: true) do |form| %>
|
11
|
+
<div class="form">
|
7
|
-
|
12
|
+
<div class="form-body">
|
13
|
+
ここから
|
8
14
|
<% @post.errors.full_messages.each do |message| %>
|
9
15
|
<div class="form-error">
|
10
16
|
<%= message %>
|
11
17
|
</div>
|
18
|
+
<% end %>
|
19
|
+
ここまで
|
20
|
+
<div>
|
21
|
+
<%= form.label :comment, "ひとこと" %>
|
22
|
+
<%= form.text_field :comment %>
|
23
|
+
</div>
|
24
|
+
|
25
|
+
<div>
|
26
|
+
<%= form.label :url, "URL" %>
|
27
|
+
<%= form.text_field :url %>
|
28
|
+
</div>
|
29
|
+
<div>
|
30
|
+
<%= form.label :content, "概要" %>
|
31
|
+
<%= form.text_area :content %>
|
32
|
+
</div>
|
33
|
+
|
34
|
+
|
35
|
+
<div>
|
36
|
+
<%= form.submit "投稿する", data: { confirm: '上記の内容で投稿を作成します。よろしいですか?' } %>
|
37
|
+
</div>
|
38
|
+
</div>
|
39
|
+
</div>
|
40
|
+
<% end %>
|
41
|
+
</div>
|
42
|
+
</div>
|
12
43
|
```
|
13
44
|
```post_controller.rb
|
45
|
+
class PostsController < ApplicationController
|
46
|
+
before_action :authenticate_user
|
47
|
+
before_action :ensure_correct_user, {only: [:destroy]}
|
48
|
+
def index
|
49
|
+
@posts = Post.all.order(created_at: :desc)
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
def show
|
54
|
+
@post = Post.find_by(id: params[:id])
|
55
|
+
@user = @post.user
|
56
|
+
@likes_count = Like.where(post_id: @post.id).count
|
57
|
+
end
|
58
|
+
|
14
|
-
def new
|
59
|
+
def new
|
15
60
|
@user = User.new
|
16
|
-
|
61
|
+
ここから
|
17
62
|
@post = Post.new
|
63
|
+
ここまで
|
64
|
+
end
|
65
|
+
|
66
|
+
def create
|
67
|
+
@post = Post.new(posts_params)
|
68
|
+
|
69
|
+
if @post.save
|
70
|
+
flash[:notice] = "投稿完了"
|
71
|
+
redirect_to("/posts/index")
|
72
|
+
else
|
73
|
+
render("/posts/new")
|
74
|
+
end
|
75
|
+
|
18
76
|
end
|
77
|
+
|
78
|
+
def posts_params
|
79
|
+
#投稿内容をパラメータで受け取る
|
80
|
+
params.permit(:comment, :site, :url, :content, :tag).merge(user_id: @current_user.id)
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
def edit
|
85
|
+
@post = Post.find_by(id: params[:id])
|
86
|
+
end
|
87
|
+
|
88
|
+
def destroy
|
89
|
+
@post = Post.find_by(id: params[:id])
|
90
|
+
@post.destroy
|
91
|
+
flash[:notice] = "投稿を削除しました"
|
92
|
+
redirect_to("/posts/index")
|
93
|
+
end
|
94
|
+
|
95
|
+
def ensure_correct_user
|
96
|
+
@post = Post.find_by(id: params[:id])
|
97
|
+
if @post.user_id != @current_user.id
|
98
|
+
flash[:notice] = "権限がありません"
|
99
|
+
redirect_to("/posts/index")
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
19
103
|
```
|
104
|
+
|
20
|
-
この2箇所を削除するとエラーなく新規投稿画面に遷移できるのですが
|
105
|
+
ここからここまでの2箇所を削除するとエラーなく新規投稿画面に遷移できるのですが
|
21
106
|
エラーの真因がわかりません。
|
22
107
|
また今までcontrollerに@postがなかったのに処理が通っていたのもよくわかりません。(create処理の@postを参照していたから?)
|
23
108
|
|
24
109
|
この真因と解決策についてご教授宜しくお願い致します。
|
25
110
|
|
111
|
+
```user.rb
|
112
|
+
class User < ApplicationRecord
|
113
|
+
# Include default devise modules. Others available are:
|
114
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
115
|
+
devise :database_authenticatable, :registerable,
|
116
|
+
:recoverable, :rememberable, :validatable, :confirmable
|
117
|
+
validates :name, {presence: true, uniqueness: true}
|
118
|
+
validates :email, {presence: true, uniqueness: true}
|
119
|
+
validates :password,{presence: true}
|
120
|
+
|
121
|
+
def posts
|
122
|
+
return Post.where(user_id: self.id)
|
123
|
+
end
|
124
|
+
|
125
|
+
def update_without_current_password(params, *options)
|
126
|
+
params.delete(:current_password)
|
127
|
+
|
128
|
+
if params[:password].blank? && params[:password_confirmation].blank?
|
129
|
+
params.delete(:password)
|
130
|
+
params.delete(:password_confirmation)
|
131
|
+
end
|
132
|
+
|
133
|
+
result = update_attributes(params, *options)
|
134
|
+
clean_up_passwords
|
135
|
+
result
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
```
|
26
|
-
```
|
140
|
+
```routes.rb
|
141
|
+
Rails.application.routes.draw do
|
142
|
+
devise_for :users, controllers: {
|
143
|
+
registrations: "users/registrations"
|
144
|
+
|
145
|
+
}
|
146
|
+
post "users/:id/update" => "users#update"
|
147
|
+
post "likes/:post_id/create" => "likes#create"
|
148
|
+
post "likes/:post_id/destroy" => "likes#destroy"
|
149
|
+
get "users/:id/edit" => "users#edit"
|
150
|
+
# post "users/create" => "users#create"
|
151
|
+
get "users/:id" => "users#show"
|
152
|
+
post "login" => "users#login"
|
153
|
+
get "login" => "users#login_form"
|
154
|
+
post "logout" => "users#logout"
|
155
|
+
|
156
|
+
|
157
|
+
get "users/:id/likes" => "users#likes"
|
158
|
+
|
159
|
+
# get "signup" => "users#new"
|
160
|
+
|
161
|
+
get "posts/index" => "posts#index"
|
27
|
-
|
162
|
+
get "posts/new" => "posts#new"
|
163
|
+
get "posts/:id" => "posts#show"
|
164
|
+
post "posts/new" => "posts#create"
|
165
|
+
post "posts/create" => "posts#create"
|
166
|
+
post "posts/:id/destroy" => "posts#destroy"
|
28
|
-
|
167
|
+
get "/" => "home#top"
|
29
|
-
<h1 class="form-heading"style="text-align: center">投稿する</h1>
|
30
|
-
<%= form_with(model: @post, local: true) do |form| %>
|
31
|
-
|
168
|
+
get "about" => "home#about"
|
32
|
-
<div class="form-body">
|
33
|
-
<% @post.errors.full_messages.each do |message| %>
|
34
|
-
<div class="form-error">
|
35
|
-
<%= message %>
|
36
|
-
</div>
|
37
|
-
<% end %>
|
38
|
-
<div>
|
39
|
-
|
169
|
+
end
|
170
|
+
|
40
171
|
```
|
41
172
|
|