创建新教程时,我正在尝试实现一个包含视频的表单。问题是我每次访问页面时都不会呈现表单。有人可以帮忙吗?
这是myapp / views / admin / tutorials / new.html.erb
<h2>New Tutorial</h2>
<%= form_for [:admin, @tutorial] do |f| %>
<%= f.label :title %>
<%= f.text_field :title, class: "block col-4 field" %>
<%= f.label :description %>
<%= f.text_area :description, class: "block col-4 field" %>
<%= f.label :thumbnail %>
<%= f.text_field :thumbnail, class: "block col-4 field" %>
<h3>Videos</h3>
<%= f.fields_for :videos do |builder| %>
<%= builder.label :youtube_url %>
<%= builder.text_field :youtube_url, class: "block col-4 field" %>
<%= builder.label :video_id %>
<%= builder.text_field :video_id, class: "block col-4 field" %>
<% end %>
<%= f.submit "Save", class: "mt2 btn btn-primary mb1 bg-teal" %>
<% end %>
这是我的视频模型
class Video < ApplicationRecord
has_many :user_videos
has_many :users, through: :user_videos
belongs_to :tutorial
validates_presence_of :position
end
还有我的教程模型
class Tutorial < ApplicationRecord
has_many :videos, -> { order(position: :ASC) }, dependent: :destroy
acts_as_taggable_on :tags, :tag_list
accepts_nested_attributes_for :videos
scope :without_classroom, -> { where(classroom: false) }
end
我的控制器:
class Admin::TutorialsController < Admin::BaseController
def new
@tutorial = Tutorial.new
end
def create
@tutorial = Tutorial.new(tutorial_params)
if @tutorial.save
flash[:success] = "Successfully created tutorial!"
redirect_to tutorial_path(@tutorial)
else
render :new
end
end
def edit
@tutorial = Tutorial.find(params[:id])
end
def update
tutorial = Tutorial.find(params[:id])
if tutorial.update(tutorial_params)
flash[:success] = "#{tutorial.title} tagged!"
end
redirect_to edit_admin_tutorial_path(tutorial)
end
private
def tutorial_params
params.require(:tutorial).permit(:tag_list, :title, :description, :thumbnail)
end
end
答案 0 :(得分:0)
什么都没呈现的原因是videos
关联为空。例如,如果您在@tutorial.videos.build
控制器操作中添加new
,那么您将获得一组字段,并且如果关联中有多个视频(无论是否持续),您将获得一组每个视频的字段数。