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

質問編集履歴

2

binding.pryの記述追加

2020/07/16 16:13

投稿

ochibi1
ochibi1

スコア1

title CHANGED
File without changes
body CHANGED
@@ -225,9 +225,29 @@
225
225
  #備考
226
226
  削除機能は正しく動いています。
227
227
  2020/07/17 追記
228
- articles_controller.rbのストロングパラメーター(article_params)を以下のように記述し直して実行してみましたが、変化ありませんでした。
228
+ articles_controller.rbのストロングパラメーター(article_params)を以下のように記述し直して実行してみましたが、変化ありませんでした。binding.pryでみてみたら、idが取れていないようでしたので、:user_idと書き換えて試してみたのですが、同じ結果になりました。。
229
+
230
+ なぜid引っ張ってこれないのでしょうか?
231
+ お力貸していただけると大変有難いです。
229
232
  ```
230
233
  def article_params
231
234
  params.require(:article).permit(:title, :body, :user)
232
235
  end
236
+ ```
237
+ ```
238
+ 20: def create
239
+ 21: @article = Article.new(article_params)
240
+ => 22: binding.pry
241
+ 23: if @article.save
242
+ 24: redirect_to @article, notice: '作成しました'
243
+ 25: else
244
+ 26: render :new, alert: '作成できませんでした'
245
+ 27: end
246
+ 28: end
247
+
248
+ [1] pry(#<ArticlesController>)> article_params
249
+ => <ActionController::Parameters {"title"=>"どうも", "body"=>"はじめまして。"} permitted: true>
250
+ [2] pry(#<ArticlesController>)> @article
251
+ => #<Article:0x00007fd7a4d863b8 id: nil, title: "どうも", body: "はじめまして。", img: nil, user_id: nil, created_at: nil, updated_at: nil>
252
+ [3] pry(#<ArticlesController>)>
233
253
  ```

1

schema.rbのDBの記載追加+ストロングパラメーターの記述変化の追加

2020/07/16 16:13

投稿

ochibi1
ochibi1

スコア1

title CHANGED
File without changes
body CHANGED
@@ -5,6 +5,36 @@
5
5
  newアクションの新規投稿画面で、新規登録ボタンを押すと、「作成しました」と表示が出ていたのですが、どこかを書き間違えたのか、入力時の画面に戻ってしまうようになりました。投稿内容がデータベースに保存されるようにしたいです。
6
6
  ど初歩的な質問です恐縮ですが、よろしくお願いします。
7
7
  #該当のコード
8
+
9
+ `schema.rb`
10
+ ```
11
+ ActiveRecord::Schema.define(version: 2020_07_15_083420) do
12
+
13
+ create_table "articles", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
14
+ t.string "title"
15
+ t.text "body"
16
+ t.string "img"
17
+ t.bigint "user_id"
18
+ t.datetime "created_at", precision: 6, null: false
19
+ t.datetime "updated_at", precision: 6, null: false
20
+ t.index ["user_id"], name: "index_articles_on_user_id"
21
+ end
22
+
23
+ create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
24
+ t.string "email", default: "", null: false
25
+ t.string "encrypted_password", default: "", null: false
26
+ t.string "nickname"
27
+ t.string "reset_password_token"
28
+ t.datetime "reset_password_sent_at"
29
+ t.datetime "remember_created_at"
30
+ t.datetime "created_at", precision: 6, null: false
31
+ t.datetime "updated_at", precision: 6, null: false
32
+ t.index ["email"], name: "index_users_on_email", unique: true
33
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
34
+ end
35
+
36
+ end
37
+ ```
8
38
  `config/routes.rb`
9
39
  ```
10
40
  Rails.application.routes.draw do
@@ -193,4 +223,11 @@
193
223
  ・ストロングパラメーターも正しく働いて、パラメーターの中身は空っぽではないはずだと思うのですが、、怪しい記述が見つけられないので、どなたかご鞭撻のほどよろしくお願いいたします。。
194
224
 
195
225
  #備考
196
- 削除機能は正しく動いています。
226
+ 削除機能は正しく動いています。
227
+ 2020/07/17 追記
228
+ articles_controller.rbのストロングパラメーター(article_params)を以下のように記述し直して実行してみましたが、変化ありませんでした。
229
+ ```
230
+ def article_params
231
+ params.require(:article).permit(:title, :body, :user)
232
+ end
233
+ ```