質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

835閲覧

Rails updateしようとするも、カラの値が保存されてしまう。

hurousyotoku500

総合スコア27

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/12/06 10:35

編集2020/12/06 13:12

やりたいこと

現在制作中のアプリのマイページ(ニックネーム、自己紹介文)を編集するページを制作しております。
ニックネーム、自己紹介文の2つの項目を同時に更新する処理になります。

発生している問題

データ自体はアップデート出来ており、完了のフラッシュも表示されているのですが、:nameと:introductionがカラになって保存されてしまいます。

イメージ説明

コード

#routes.rb get "/mypages/index",to: 'mypages#index' ,as: "mypage" get "/mypages/edit",to: 'mypages#edit' ,as: "mypage_edit" put "/mypages" ,to: 'messages#update' ,as: "mypage_update"
#users_controller.rb def update @user = User.find(current_user.id) if @user.update(name: params[:name], introduction: params[:introduction]) flash[:edit_success] = '編集されました' redirect_to mypage_edit_path else flash.now[:danger] = 'スコアが編集されませんでした' render 'archives/show' end end
#edit.html <%= form_with(model: @user, local: true) do |f| %> <%= f.label :name, 'ニックネーム' %> <%= f.text_field :name %><br> <%= f.label :introduction, '自己紹介文' %> <%= f.text_area :introduction %><br> <%= f.submit "編集する" %> <% end %>

送信時のログ

Started GET "/mypages/edit" for ::1 at 2020-12-06 21:58:07 +0900
Processing by MypagesController#edit as HTML
User Load (0.7ms) SELECT users.* FROM users WHERE users.id = 3 ORDER BY users.id ASC LIMIT 1
↳ app/controllers/mypages_controller.rb:6:in edit' User Load (0.4ms) SELECT users.* FROM usersWHEREusers.id= 3 LIMIT 1 ↳ app/controllers/mypages_controller.rb:6:inedit'
Rendering mypages/edit.html.erb within layouts/application
Rendered mypages/edit.html.erb within layouts/application (Duration: 8.1ms | Allocations: 418)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_flash.scores.html.erb (Duration: 0.4ms | Allocations: 18)
Completed 200 OK in 61ms (Views: 40.7ms | ActiveRecord: 1.2ms | Allocations: 6060)

Started GET "/mypages/edit" for ::1 at 2020-12-06 21:58:23 +0900
Processing by MypagesController#edit as HTML
User Load (3.1ms) SELECT users.* FROM users WHERE users.id = 3 ORDER BY users.id ASC LIMIT 1
↳ app/controllers/mypages_controller.rb:6:in edit' User Load (1.4ms) SELECT users.* FROM usersWHEREusers.id= 3 LIMIT 1 ↳ app/controllers/mypages_controller.rb:6:inedit'
Rendering mypages/edit.html.erb within layouts/application
Rendered mypages/edit.html.erb within layouts/application (Duration: 1.8ms | Allocations: 427)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_flash.scores.html.erb (Duration: 0.0ms | Allocations: 18)
Completed 200 OK in 40ms (Views: 27.6ms | ActiveRecord: 4.5ms | Allocations: 6076)

Started PATCH "/users/3" for ::1 at 2020-12-06 21:58:27 +0900
Processing by UsersController#update as HTML
Parameters: {"authenticity_token"=>"X9EBAkaYwjpkC7MgwRU8/8otTrZjeCOA1vbQIUvP+10AQ0iajpemNKzNn0y3FLXA2ZMgJFXL093hkcDurgK6CA==", "user"=>{"name"=>"あや", "introduction"=>"あやですよ。"}, "commit"=>"編集する", "id"=>"3"}
User Load (5.6ms) SELECT users.* FROM users WHERE users.id = 3 ORDER BY users.id ASC LIMIT 1
↳ app/controllers/users_controller.rb:35:in update' User Load (0.7ms) SELECT users.* FROM usersWHEREusers.id= 3 LIMIT 1 ↳ app/controllers/users_controller.rb:35:inupdate'
(6.5ms) BEGIN
↳ app/controllers/users_controller.rb:36:in update' User Update (4.6ms) UPDATE usersSETusers.name= NULL,users.introduction= NULL,users.updated_at= '2020-12-06 12:58:27.921384' WHEREusers.id= 3 ↳ app/controllers/users_controller.rb:36:inupdate'
(6.1ms) COMMIT
↳ app/controllers/users_controller.rb:36:in `update'
Redirected to http://localhost:3000/mypages/edit
Completed 302 Found in 43ms (ActiveRecord: 23.5ms | Allocations: 4740)

Started GET "/mypages/edit" for ::1 at 2020-12-06 21:58:27 +0900
Processing by MypagesController#edit as HTML
User Load (3.0ms) SELECT users.* FROM users WHERE users.id = 3 ORDER BY users.id ASC LIMIT 1
↳ app/controllers/mypages_controller.rb:6:in edit' User Load (0.4ms) SELECT users.* FROM usersWHEREusers.id= 3 LIMIT 1 ↳ app/controllers/mypages_controller.rb:6:inedit'
Rendering mypages/edit.html.erb within layouts/application
Rendered mypages/edit.html.erb within layouts/application (Duration: 1.0ms | Allocations: 418)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_flash.scores.html.erb (Duration: 0.1ms | Allocations: 19)
Completed 200 OK in 17ms (Views: 7.1ms | ActiveRecord: 3.4ms | Allocations: 6076)

やったみたこと

emailアドレスの変更もしようとしていましたが、deviseの影響かわかりませんが、バリデーションで弾かれたので、今回は名前と自己紹介文のみとしました。
ログをみると、UPDATE users SET users.name = NULL, users.introduction = NULL, と書いてあり、update出来ていることはわかるのですが、値が全てNULLとなってしまっています。view側から値が渡っていないと思われます。

ひとつ気になっているのは、viewは/mypages/editなのですが、controllerはusers_controllerの#updateで処理させているのですが、問題ないのでしょうか。

情報不足かもしれませんが、宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

logでparamater見てください
"user"=>{"name"=>"あや", "introduction"=>"あやですよ。"},
です。
params[:name] ではなく params[:user][:name] です。
ただ、ここはRailsが用意している strongparamater使うことを薦めます。

投稿2020/12/06 21:49

winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hurousyotoku500

2020/12/07 00:27

ありがとうございます。無事更新できました。 ですが、なぜ、[:user]がつくのか理由がわかりません。
winterboum

2020/12/07 00:39

view と できた htmlと見てください
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問