前提・実現したいこと
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
ここの記載方法が違うのでしょうがネットで調べても、同じ記載をしています。
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー