我尝试将多个图片和视频添加到我的模型演示文稿中,但是当我提交表单时,控制台会说:
Unpermitted parameters: video
Unpermitted parameters: image
presentation.rb
class Presentation < ActiveRecord::Base
belongs_to :formation
has_many :videos, :dependent => :destroy
has_many :slides, :dependent => :destroy
accepts_nested_attributes_for :videos
accepts_nested_attributes_for :slides
end
slide.rb
class Slide < ActiveRecord::Base
belongs_to :presentation
has_attached_file :image
do_not_validate_attachment_file_type :image
end
video.rb :
class Video < ActiveRecord::Base
belongs_to :presentation
has_attached_file :video
do_not_validate_attachment_file_type :video
end
PresentationController :
class PresentationController < ApplicationController
def new
@formation = Formation.find(params[:id])
@presentation = Presentation.new
@presentation.videos.build
@presentation.slides.build
end
def create
@formation = Formation.find(params[:id])
@presentation = @formation.presentations.new(presentation_params)
if @presentation.save
redirect_to formations_show_path(@formation)
else
render action: 'new'
end
end
def destroy
@formation = Formation.find(params[:id])
Presentation.find(params[:pres]).destroy
redirect_to formations_show_path(@formation)
end
private
def presentation_params
params.require(:presentation).permit(:name, :description,videos_attributes: [:video], slides_attributes: [:image])
end
end
表格:
<div class="container contenu">
<div class="row">
<h1>Création d'une présentation</h1>
<%= form_for(@presentation, :url => presentations_create_path, html: { multipart: true }, :class=>"form-horizontal") do |f| %>
<% if @presentation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@presentation.errors.count, "error") %> prohibited this survey from being saved:</h2>
<ul>
<% @presentation.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name, :class => "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :name, :class =>"form-control" %>
</div>
</div>
<div class="form-group">
<%= f.label :description, :class => "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_area :description, :row => "3",:class =>"form-control" %>
</div>
</div>
<div class="form-group">
<%= f.fields_for :videos do |v| %>
<%= v.label :video, :class => "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= v.file_field :video, multiple: true, :class =>"form-control"%>
</div>
<% end %>
</div>
<div class="form-group">
<%= f.fields_for :slides do |s| %>
<%= s.label :image, :class => "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= s.file_field :image, multiple: true, :class =>"form-control"%>
</div>
<% end %>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%= f.submit :class => "btn btn-default"%>
</div>
</div>
<% end %>
<%= link_to 'retour', formations_show_path(@formation) %>
</div>
</div>
我该怎么做才能避免Unpermitted parameter
错误?
当我尝试只有一张幻灯片和一个视频时,它没问题。但是当我在表单上添加多个:true时它不起作用。
由于
更新 没关系,我试图修改表格上的文件输入名称
<%= v.file_field :video, multiple: true,:name =>"presentation[videos_attributes][][video]", :class =>"form-control"%>
它有效:)
斯克恩。