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

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

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

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Ruby on Rails

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

Q&A

0回答

1054閲覧

formオブジェクトを使用した際の保存方法

akari0509

総合スコア3

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Ruby on Rails

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

0グッド

1クリップ

投稿2020/09/28 06:56

保存機能を実装したい

現在、野球のメンバー登録とめんばー表のさくせいを実装しています。
メンバーの登録に関しては実装できましたが、メンバー表の作成ができません。゚(゚´ω`゚)゚。
formオブジェクトを使用して登録済みのメンバーをメンバー表に反映させて保存したいのですが下記のエラーが出ています。

発生している問題・エラーメッセージ

エラー内容
ActionController::ParameterMissing in OrdersController#create
param is missing or the value is empty: order_defence

該当のソースコード

以下の記述のストロングパラメーターの記述でvalueが無いと怒られています。

###メンバー表作成のコントローラー class OrdersController < ApplicationController def index @member = Member.all @order = OrderDefence.new end def create @order = OrderDefence.create(order_params) if @order.save return redirect_to root_path else render "index" end end private def order_params params.require(:order_defence).permit(:match_name, :year_id, :month_id, :day_id, :first_name_id, :first_position_id, :second_name_id, :second_position_id, :third_name_id, :third_position_id, :fourth_name_id, :fourth_position_id, :fifth_name_id, :fifth_position_id, :sixth_name_id, :sixth_position_id, :seventh_name_id, :seventh_position_id, :eighth_name_id, :eight_position_id, :ninth_name_id, :ninth_position_id, :user_id, :member_id).merge(user_id: current_user.id, member_id: @member) end end
###formオブジェクト class OrderDefence include ActiveModel::Model attr_accessor :match_name, :year_id, :month_id, :day_id, :first_name_id, :first_position_id, :second_name_id, :second_position_id, :third_name_id, :third_position_id, :fourth_name_id, :fourth_position_id, :fifth_name_id, :fifth_position_id, :sixth_name_id, :sixth_position_id, :seventh_name_id, :seventh_position_id, :eighth_name_id, :eight_position_id, :ninth_name_id, :ninth_position_id def save match = Match.create(match_name: match_name, year_id: year_id, month_id: month_id, day_id: day_id, user_id: user_id) First.create(first_name_id: first_name_id, first_position_id: first_position_id, member_id: @member.id, metch_id: match.id) Second.create(second_name_id: second_name_id, second_position_id: second_position_id, member_id: @member.id, metch_id: match.id) Third.create(third_name_id: third_name_id, third_position_id: third_position_id, member_id: @member.id, metch_id: match.id) Fourth.create(fourth_name_id: fourth_name_id, fourth_position_id: fourth_position_id, member_id: @member.id, metch_id: match.id) Fifth.create(fifth_name_id: fifth_name_id, fifth_position_id: fifth_position_id, member_id: @member.id, metch_id: match.id) Sixth.create(sixth_name_id: sixth_name_id, sixth_position_id: sixth_position_id, member_id: @member.id, metch_id: match.id) Seventh.create(seventh_name_id: seventh_name_id, seventh_position_id: seventh_position_id, member_id: @member.id, metch_id: match.id) Eighth.create(eighth_name_id: eighth_name_id, eighth_position_id: eighth_position_id, member_id: @member.id, metch_id: match.id) Ninth.create(ninth_name_id: ninth_name_id, ninth_position_id: ninth_position_id, member_id: @member.id, metch_id: match.id) end end
###memberモデル(選手の登録) class Member < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions belongs_to_active_hash :style belongs_to :user has_one_attached :image has_one :first has_one :second has_one :third has_one :fourth has_one :fifth has_one :sixth has_one :seventh has_one :eight has_one :ninth validates :image, :name, :age, :style_id, :number, presence: true validates :style_id, numericality: { other_than: 0 } end
###matchモデル(試合の日程や対戦相手保存する) class Match < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions belongs_to_active_hash :year belongs_to_active_hash :month belongs_to_active_hash :day belongs_to :user has_one :first has_one :second has_one :third has_one :fourth has_one :fifth has_one :sixth has_one :seventh has_one :eight has_one :ninth # validates :match_name, :year_id, :month_id, :day_id, presence: true # validates :year_id, :month_id, :day_id, numericality: { other_than: 0 } end
###pitcherモデル(1番目の選手) ###以下のモデルが合計9個あります。 class First < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions belongs_to_active_hash :first_position belongs_to :member belongs_to :match end

試したこと

記述が多くなってしまい申し訳ございません。
binding.pryを試みたのですが、カラムの情報は入っています。paramsにはuser_idが入っておらず、paramitted_folseと
なっています。
うまく説明ができず申し訳ないですが、お答えいただけると幸いです。
また記述や情報に関して足りない点などありましたらすぐに回答いたします。
何卒、宜しくお願いいたします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問