teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

raiseで試した時の内容

2019/05/03 13:37

投稿

stks56
stks56

スコア15

title CHANGED
File without changes
body CHANGED
@@ -147,4 +147,22 @@
147
147
  has_many :users, through: :likes
148
148
  end
149
149
 
150
- ```
150
+ ```
151
+
152
+ 追記
153
+ ```
154
+ def create
155
+ console
156
+ @post = Post.new(post_params)
157
+ @post.user_id = current_user.id
158
+ if @post.save
159
+ flash[:notice] = "記事を投稿しました"
160
+ redirect_to("/posts")
161
+ else
162
+ raise
163
+ render("posts/new")
164
+ end
165
+ end
166
+ ```
167
+ とした時のエラー内容です
168
+ ![イメージ説明](abea58000e86eeb598c3cec0955af33f.png)

1

Postモデルのバリデーションを追記

2019/05/03 13:37

投稿

stks56
stks56

スコア15

title CHANGED
File without changes
body CHANGED
@@ -128,4 +128,23 @@
128
128
  form自体は正常に投稿できます。
129
129
 
130
130
  アドバイスいただけると幸いです。
131
- よろしくお願いします。
131
+ よろしくお願いします。
132
+
133
+ 追記
134
+ Postモデルのバリデーション内容です
135
+ ```
136
+ class Post < ApplicationRecord
137
+ validates :title, {presence: true}
138
+ validates :content, {presence: true}
139
+ validates :game_id, {presence: true}
140
+ validates :user_id, {presence: true}
141
+
142
+ belongs_to :user
143
+ belongs_to :game
144
+
145
+ has_many :likes
146
+ accepts_nested_attributes_for :likes
147
+ has_many :users, through: :likes
148
+ end
149
+
150
+ ```