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

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

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

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

Ruby on Rails

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

Q&A

0回答

847閲覧

rails 親子関係のあるモデルを同時登録するform機能について

yuuuta

総合スコア1

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

Ruby on Rails

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

0グッド

0クリップ

投稿2020/05/22 10:16

前提・実現したいこと

railsの質問です。
親子関係のあるモデルを同時登録するformを作っています。
親category 子video
難しいところではないのですが、エラーの原因がわからず困っています。

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

以下のエラーが出ます。

Processing by Admin::CategoriesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7/B3xd4ZptGUf5ySZJERLADaQSDOWNvTrh68e5LIrZXwOCdIZgFFR1O3X9W+nPismVyybiTYbUKlaR1MONoFYA==", "category"=>{"title"=>"sssas", "category_type_id"=>"1", "short_description"=>"ddffh", "video"=>{"title"=>"hhgggf"}}, "commit"=>"作成"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Unpermitted parameter: :video

該当のソースコード

親のモデルコード
class Category < ApplicationRecord
validates :title, presence: true
validates :short_description, presence: false
validates :description, presence: false

belongs_to :category_type
has_many :questionnaires
has_many :questionnaire_answers
has_many :videos
accepts_nested_attributes_for :videos

子のモデルコード
class Video < ApplicationRecord
validates_presence_of :title
belongs_to :category
has_many :comments
has_many :progresses
has_many :video_checksheets
has_many :checksheets, through: :video_checksheets
has_one :checksheet_answer

has_attached_file :videofile
validates_attachment :videofile, content_type: { content_type: ['video/mp4'] }

has_attached_file :worksheet
has_attached_file :slide

has_attached_file :thumbnail, styles: { small: '320x320>' }
validates_attachment_content_type :thumbnail, content_type: /\Aimage/(png|jpeg|jpg)\z/

validates :videofile, attachment_presence: true
validates :thumbnail, attachment_presence: true
validates :minutes, presence: true
validates :seconds, presence: true

validates :minutes, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :seconds, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than: 60 }

コントローラ

class Admin::CategoriesController < Admin::Base
before_action :set_category, only: [:show, :edit, :update, :destroy]

def index
@categories = Category.all.order(:id)
@category_types = CategoryType.all.order(:id)
end

GET /categories/new

def new
@category = Category.new
@category.videos.build
@text = '作成'
end

GET /categories/1/edit

def edit
@text = '更新'
end

POST /categories

POST /categories.json

def create
@category = Category.new(category_params)

respond_to do |format| if @category.save format.html { redirect_to admin_categories_path, notice: 'Category was successfully created.' } else format.html { render :new } end end

end

def update
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to admin_categories_path, notice: 'Category was successfully updated.' }
else
format.html { render :edit }
end
end
end

def destroy
@category.destroy
respond_to do |format|
format.html { redirect_to admin_categories_path, notice: 'Category was successfully destroyed.' }
end
end

private
def set_category
@category = Category.find(params[:id])
end

def category_params params.require(:category).permit(:title, :short_description, :category_type_id,     videos_attributes: [:title]) end

end

ビュー

<%= form_with(model: [:admin, @category]) do |f| %>

<div class="field"> <%= f.label '講座名:' %> <%= f.text_field :title %><br> <%= f.label 'カテゴリ:' %> <%= f.collection_select :category_type_id, CategoryType.all, :id, :title, {}, style: 'width:150px' %><br> <%= f.label '説明' %><br> <%= f.text_area :short_description, size: '100x5' %><br> </div> <h1>チャプター項目編集</h1> <%= f.fields_for :video do |g| %> <div class="field"> <%= g.label 'チャプター名' %> <%= g.text_field :title %><br> </div> <% end %> <div class="actions"> <%= f.submit @text, class: "btn btn-primary" %> </div> <% end %>

試したこと

ここに問題に対して試したことを記載してください。
def category_params
params.require(:category).permit(:title, :short_description, :category_type_id,     videos_attributes: [:title])
end
ここの記載方法が違うのでしょうがネットで調べても、同じ記載をしています。

ここにより詳細な情報を記載してください。

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

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

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

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

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

winterboum

2020/05/23 03:01 編集

1) それはエラーではありません。たんなるlog、経過報告です。 2) codeがベタなので読みにくく、読む気が起きません。<code>を使って読みやすくしてください file名もわかるように。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問