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

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

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

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

Q&A

解決済

1回答

613閲覧

rails 1対1form updateが成功しない

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails

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

0グッド

0クリップ

投稿2021/01/13 08:32

編集2021/01/13 08:56

前提・実現したいこと

railsの1対1リレーションがあるフォームを作成しています。
エラーは出ていないのですが、updateが成功していないため、原因を知りたいです。

発生している問題

エラーは出ていないが、updateされずに:editにレンダリングされる. updateが成功すれば、期待通りにリダイレクトするはずです。

controller

1def edit 2 @property = Property.find(params[:id]) 3 end 4 5 def update 6 @property = Property.find(params[:id]) 7 logger.debug(property_params) 8 if @property.update(property_params) 9 flash[:success] = "Profile updated" 10 redirect_to property_path(@property) 11 else 12 render 'edit' 13 end 14 end 15 16 private 17 18 def property_params 19 params.require(:property).permit(:name, property_overview_attributes: [:address, :postal_code, :date_of_build]) 20 end

form

1<%= form_with(model: @property, local: true) do |f| %> 2 3 <div class="form-group"> 4 <%= f.label :name %> 5 <%= f.text_field :name, class: 'form-control' %> 6 </div> 7 8 <%= f.fields_for(:property_overview, local: true) do |i| %> 9 <div class="form-group"> 10 <%= i.label :address %> 11 <%= i.text_field :address, class: 'form-control' %> 12 </div> 13 14 <div class="form-group"> 15 <%= i.label :postal_code %> 16 <%= i.number_field :postal_code, class: 'form-control' %> 17 </div> 18 19 <div class="form-group"> 20 <%= i.label :date_of_build %> 21 <%= i.date_field :date_of_build, class: 'form-control' %> 22 </div> 23 <% end %> 24 <%= f.submit yield(:button_text), class: "btn btn-primary" %> 25<% end %>

log

1Started PATCH "/properties/10" for 172.18.0.1 at 2021-01-13 08:22:29 +0000 2Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1 3Processing by PropertiesController#update as HTML 4 Parameters: {"authenticity_token"=>"ua3FliJrkPp0pZa2tHECWtJTUWngU3LXEy3T+A6tnHfvIfsf+ybHPXGGNzH/darZeFeEJcsY3EMVzmV8q33KrQ==", "property"=>{"name"=>"kamana", "property_overview_attributes"=>{"address"=>"アップデートできないのはなぜ?", "postal_code"=>"198738", "date_of_build"=>"2021-01-06", "id"=>"10"}}, "commit"=>"Save Changes", "id"=>"10"} 5 User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] 6 ↳ app/helpers/sessions_helper.rb:9:in `current_user' 7 UserAuthorization Exists? (0.3ms) SELECT 1 AS one FROM "user_authorizations" WHERE "user_authorizations"."user_id" = $1 AND "user_authorizations"."controller" = $2 AND "user_authorizations"."action" = $3 LIMIT $4 [["user_id", 1], ["controller", "properties"], ["action", "update"], ["LIMIT", 1]] 8 ↳ app/controllers/application_controller.rb:22:in `check_authorization' 9 Property Load (0.2ms) SELECT "properties".* FROM "properties" WHERE "properties"."id" = $1 LIMIT $2 [["id", 10], ["LIMIT", 1]] 10 ↳ app/controllers/properties_controller.rb:30:in `update' 11Unpermitted parameter: :id 12  (0.5ms) BEGIN 13 ↳ app/controllers/properties_controller.rb:32:in `update' 14 PropertyOverview Load (0.6ms) SELECT "property_overviews".* FROM "property_overviews" WHERE "property_overviews"."property_id" = $1 LIMIT $2 [["property_id", 10], ["LIMIT", 1]] 15 ↳ app/controllers/properties_controller.rb:32:in `update' 16 PropertyOverview Destroy (0.6ms) DELETE FROM "property_overviews" WHERE "property_overviews"."id" = $1 [["id", 10]] 17 ↳ app/controllers/properties_controller.rb:32:in `update' 18 Property Destroy (0.9ms) DELETE FROM "properties" WHERE "properties"."id" = $1 [["id", 10]] 19 ↳ app/controllers/properties_controller.rb:32:in `update' 20 PropertyOverview Exists? (0.5ms) SELECT 1 AS one FROM "property_overviews" WHERE "property_overviews"."address" = $1 LIMIT $2 [["address", "アップデートできないのはなぜ?"], ["LIMIT", 1]] 21 ↳ app/controllers/properties_controller.rb:32:in `update' 22  (0.4ms) ROLLBACK 23 ↳ app/controllers/properties_controller.rb:32:in `update' 24 Rendering properties/edit.html.erb within layouts/application 25 Rendered layouts/_side.html.erb (Duration: 0.2ms | Allocations: 68) 26 Rendered shared/_error_message.html.erb (Duration: 0.1ms | Allocations: 5) 27 Rendered properties/_form.html.erb (Duration: 5.0ms | Allocations: 1381) 28 Rendered properties/edit.html.erb within layouts/application (Duration: 8.7ms | Allocations: 1584) 29[Webpacker] Everything's up-to-date. Nothing to do 30 Rendered layouts/_rails_default.html.erb (Duration: 43.6ms | Allocations: 13894) 31Completed 200 OK in 133ms (Views: 57.1ms | ActiveRecord: 14.2ms | Allocations: 38662)

createやdestroyなど、他のアクションは期待通りに動いています。
足りない情報があれば教えていただきたいです。

個人的には、logに含まれる

Unpermitted parameter: :id

がきになるのですが、これは何者でしょうか?
原因がわかれば、お力添えいただきたいです。よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

Unpermitted parameter: :id

なので、
property_paramsにidを追加しました。

def property_params params.require(:property).permit(:name, property_overview_attributes: [id, :address, :postal_code, :date_of_build]) end

投稿2021/01/13 09:12

編集2021/01/13 09:54
退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問