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

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

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

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

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Q&A

解決済

1回答

516閲覧

rails5 CRUD処理 エラー画面 原因が分かりません

yu_yu

総合スコア7

Ruby

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

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

0グッド

0クリップ

投稿2020/05/15 08:04

編集2020/05/15 09:46

CRUD処理を使ったメモアプリを作成中です。
フォームに入力を行い、作成ボタンを押すと下記のようなエラーになってしまいす。
エラーの原因分かる方いましたらよろしくお願いします。

Rails ver は5.0.7.2です。
親:User テーブル (company,userid,pass,memo)
子:Schedule テーブル (time,user_id,category_id)
Category テーブル (category)

イメージ説明

ruby

1発生しているエラー 2 3 4Started POST "/users" for 42.146.57.23 at 2020-05-15 09:45:00 +0000 5Cannot render console from 42.146.57.23! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 6Processing by UsersController#create as HTML 7 Parameters: {"utf8"=>"✓", "authenticity_token"=>"oHM11OL6+8sV5fPU0+/m2IL7jwSmzgOledIMmPXfApyGDgADQlhnahbChTYfMbqowReE7wQUoipJC5fLysPniA==", "user"=>{"company"=>"株式会社A", "userid"=>"ユーザーA", "pass"=>"パスワードE", "memo"=>"メモを追加していく", "schedules_attributes"=>{"0"=>{"category_id"=>"1", "time"=>"15時までに提出", "_destroy"=>"false"}}}, "commit"=>"作成"} 8 (0.1ms) begin transaction 9 (0.0ms) rollback transaction 10 Rendering users/new.html.erb within layouts/application 11 Category Load (0.1ms) SELECT "categories".* FROM "categories" 12 CACHE (0.0ms) SELECT "categories".* FROM "categories" 13 Rendered users/_form.html.erb (5.3ms) 14 Rendered users/new.html.erb within layouts/application (6.4ms) 15Completed 200 OK in 52ms (Views: 47.7ms | ActiveRecord: 0.3ms) 16 17

ruby

1#_form.hyml.erb 2<%= nested_form_for(@user) do |f| %> 3 <% if user.errors.any? %> 4 <div id="error_explanation"> 5 <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2> 6 7 <ul> 8 <% user.errors.full_messages.each do |message| %> 9 <li><%= message %></li> 10 <% end %> 11 </ul> 12 </div> 13 <% end %> 14 15 16 <div class="field"> 17 <%= f.label "企業名" %> 18 <%= f.text_field :company,class:"form-control",placeholder:"企業名" %> 19 </div> 20 21 <div class="field"> 22 <%= f.label "ユーザーID" %> 23 <%= f.text_field :userid,class:"form-control",placeholder:"ユーザーID" %> 24 </div> 25 26 <div class="field"> 27 <%= f.label "パスワード" %> 28 <%= f.text_field :pass,class:"form-control",placeholder:"パスワード" %> 29 </div> 30 31 <div class="field"> 32 <%= f.label "メモ" %> 33 <%= f.text_field :memo,class:"form-control",placeholder:"メモ" %> 34 </div> 35 36 37 38 <%= f.fields_for :schedules do |f| %> 39 <hr class=hr-tag> 40 <div class="field"> 41 <%= f.collection_select :category_id, Category.all, :id, :name,{:prompt => "予定を選択"},class:"form-control",id:"select-form"%> 42 <%= f.text_field :time, class:"form-control",placeholder:"日時"%> 43 </div> 44 <div class="buttons"> 45 <%= f.link_to_remove do%> 46 <span class="glyphicon glyphicon-trash color-blue"></span> 47 </div> 48 49 <% end %> 50 <% end %> 51 52 53 <%= f.link_to_add "予定の追加", :schedules ,class:"button-4-1"%> 54 <%= f.submit "作成",class:"button-4-1"%> 55 56 57 58<% end %> 59 60 61

ruby

1#users_controller 2class UsersController < ApplicationController 3 before_action :set_user, only: [:show, :edit, :update, :destroy] 4 5 6 # GET /users 7 # GET /users.json 8 def index 9 @users = User.all 10 @schedules=Schedule.all 11 end 12 13 # GET /users/1 14 # GET /users/1.json 15 def show 16 end 17 18 # GET /users/new 19 def new 20 @user = User.new 21 @schedule = @user.schedules.build 22 23 end 24 25 # GET /users/1/edit 26 def edit 27 end 28 29 # POST /users 30 # POST /users.json 31 def create 32 @user = User.new(user_params) 33 34 respond_to do |format| 35 if @user.save 36 format.html { redirect_to @user, notice: 'User was successfully created.' } 37 format.json { render :show, status: :created, location: @user } 38 else 39 format.html { render :new } 40 format.json { render json: @user.errors, status: :unprocessable_entity } 41 end 42 end 43 end 44 45 # PATCH/PUT /users/1 46 # PATCH/PUT /users/1.json 47 def update 48 respond_to do |format| 49 if @user.update(user_params) 50 format.html { redirect_to @user, notice: 'User was successfully updated.' } 51 format.json { render :show, status: :ok, location: @user } 52 else 53 format.html { render :edit } 54 format.json { render json: @user.errors, status: :unprocessable_entity } 55 end 56 end 57 end 58 59 # DELETE /users/1 60 # DELETE /users/1.json 61 def destroy 62 @user.destroy 63 respond_to do |format| 64 format.html { redirect_to users_url, notice: 'User was successfully destroyed.' } 65 format.json { head :no_content } 66 end 67 end 68 69 private 70 # Use callbacks to share common setup or constraints between actions. 71 def set_user 72 @user = User.find(params[:id]) 73 end 74 75 # Only allow a list of trusted parameters through. 76 def user_params 77 params.require(:user).permit(:company, :userid, :pass, :memo,schedules_attributes: [:id, :time, :user_id, :category_id,:_destroy ] ) 78 end 79end 80 81

ruby

1#user.rb 2class User < ApplicationRecord 3 has_many :schedules, dependent: :destroy 4 accepts_nested_attributes_for :schedules, allow_destroy: true 5 6end 7 8 9#schedule.rb 10class Schedule < ApplicationRecord 11 belongs_to :user 12end 13 14 15#category.rb 16class Category < ApplicationRecord 17end 18 19

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

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

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

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

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

yu_yu

2020/05/15 09:17

追加いたしました。よろしくお願いします。
winterboum

2020/05/15 09:34

そのエラーはScheduleがbelongs_to :userであるのにuser_idが落ちてるというものなので、 accepts_nested_attributes_for  がないのでは?と思ったのですが、ありますね。。。。
yu_yu

2020/05/15 09:37

回答ありがとうございます。 #users_controller の `user_params` の部分はあっているでしょうか?
winterboum

2020/05/15 09:38

合っているようにみえます。 ああ言うの画像でなくtextで欲しい
yu_yu

2020/05/15 09:47

失礼いたしました。エラー部分を画像から修正いたしました。
yu_yu

2020/05/15 17:26

自己解決することができました。回答いただきありがとうございました。
guest

回答1

0

自己解決

shedule.rbbelongs_to :useroptional: true を付けた所保存できるようになりました。まだプログラミング経験が浅いため、解釈の仕方があっているかわかりませんが一応書いておきます。

【 考察 】
データを保存する際、親子関係にあるため親(Userテーブル)のIDが決まってからでないと子(Scheduleテーブル)の情報を正常に保存できません。しかしoptional: trueを入れていなかったため子テーブルの情報から保存しようとしていました。そのため正常に保存できなかったのではないかと思います。
参考サイト:「https://qiita.com/a-itabashi/items/6e0d4793996d887fd191」

ruby

1修正前 2#schedule.rb 3class Schedule < ApplicationRecord 4 belongs_to :user 5end 6

ruby

1修正後 2#schedule.rb 3class Schedule < ApplicationRecord 4 belongs_to :user,optional: true 5end

投稿2020/05/15 17:38

編集2020/05/16 05:10
yu_yu

総合スコア7

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問