Rails - 如何通过表单动态嵌入 wistia 视频

Rails - How to embed a wistia video dynamically through a form

我正在制作一个电子学习平台并拥有课程资源,因此我可以动态添加课程。我现在正处于需要给每个课程上传视频的阶段,我决定使用wistia来完成这个。

我不确定的是如何直接通过课程表格嵌入属于每门课程的视频?我找不到有关如何实现此目的的文档,因此非常感谢任何帮助。

示例:-

我有来自 wistia 的嵌入代码:

<iframe src="//fast.wistia.net/embed/iframe/tk6picrt4k" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" allowfullscreen mozallowfullscreen webkitallowfullscreen oallowfullscreen msallowfullscreen width="640" height="388"></iframe>
<script src="//fast.wistia.net/assets/external/E-v1.js" async></script>

一旦我在数据库中创建了 :video_url table,如果我将上面的代码粘贴到“创建课程”操作的表单框中并提交形式?以及 :video_url 应该是什么数据类型?

课程管理员:-

class CoursesController < ApplicationController
  before_action :set_course, only: [:show, :edit, :update, :destroy]
  before_action :set_grade
  respond_to :html


  def index
   @grades = Grade.all
   @courses = Course.where grade: params[:grade_id]
   respond_with(@course)
  end


  def show
   respond_with(@course)
  end


  def new
    @grade = Grade.find(params[:grade_id])
    @course = @grade.courses.new
    respond_with(@course)
  end


  def edit

  end


  def create
    @grade = Grade.find(params[:grade_id])
    @course = @grade.courses.build(course_params)
    if @course.save
      redirect_to grade_courses_path(@grade, @courses), notice: 'Successfully Created Course'
    else
      render action: 'new'
    end
  end


  def update
    @course.update(course_params)
    redirect_to grade_courses_path(@grade, @courses), :notice => "Successfully Updated Course"
  end


  def destroy
    @course.destroy
    redirect_to grade_courses_path(@grade, @courses), :notice => "Successfully Deleted Course"
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_course
      @course = Course.find(params[:id])
    end

    def set_grade
      @grade = Grade.find(params[:grade_id])
    end 

    # Never trust parameters from the scary internet, only allow the white list through.
    def course_params
      params.require(:course).permit(:title, :period, :description, :semester, :grade, :subject, :grade_id, :chapters_from, :chapters_to, :instructor, :price)
    end
end

_form.html.erb

<%= bootstrap_form_for [@grade, @course] do |f| %>
  <% if @course.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@course.errors.count, "error") %> prohibited this course from being saved:</h2>

      <ul>
      <% @course.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.select :subject, [["Math"], ["Physics"], ["Chemistry"], ["Science"], ["Biology"], ["English"], ["Arabic"], ["Social Studies"]], { label: "Subject"}, { class: "selectpicker" } %>
  </div>
  <div class="field">
    <%= f.text_field :instructor %>
  </div>
  <div class="field">
    <%= f.select :period, [["First"], ["Second"], ["Third"], ["Final"]], { label: "Period"}, { class: "selectpicker" } %>
  </div>
  <div class="field">
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.select :semester, [["First"], ["Second"]], { label: "Semester"}, { class: "selectpicker" } %>
  </div>
  <div class="field">
    <%= f.number_field :chapters_from %>
  </div>
  <div class="field">
    <%= f.number_field :chapters_to %>
  </div>
  <div class="field">
    <%= f.number_field :price %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

在您的控制器中,我们必须允许设置新属性 video_url。 Rails 将自行处理 updates/creates:

# Never trust parameters from the scary internet, only allow the white list through.
def course_params
  params.require(:course).permit(:title, :period, :description, :semester, :grade, :subject, :grade_id, :chapters_from, :chapters_to, :instructor, :price, :video_url)
end

在您拥有静态 iframe 的视图中:

...
<iframe src="#{@course.video_url}" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" allowfullscreen mozallowfullscreen webkitallowfullscreen oallowfullscreen msallowfullscreen width="640" height="388"></iframe>
...

这将使用给定的 video_url 填充您的视图。注意,如果 video_url 是 NIL.

,则没有异常处理

您的 video_url 数据类型应为 :string。对于简单的 URL :)

就足够了

考虑到您已经有一个表格来为您的课程提交数据(创建、编辑、更新课程信息),您只需为 URL 添加另一个 input fieldvideo_url).


既然您提供了表单,我们可以再添加一个输入:

<%= bootstrap_form_for [@grade, @course] do |f| %>
  <% if @course.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@course.errors.count, "error") %> prohibited this course from being saved:</h2>

      <ul>
      <% @course.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.select :subject, [["Math"], ["Physics"], ["Chemistry"], ["Science"], ["Biology"], ["English"], ["Arabic"], ["Social Studies"]], { label: "Subject"}, { class: "selectpicker" } %>
  </div>
  <div class="field">
    <%= f.text_field :instructor %>
  </div>
  <div class="field">
    <%= f.select :period, [["First"], ["Second"], ["Third"], ["Final"]], { label: "Period"}, { class: "selectpicker" } %>
  </div>
  <div class="field">
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.select :semester, [["First"], ["Second"]], { label: "Semester"}, { class: "selectpicker" } %>
  </div>
  <div class="field">
    <%= f.number_field :chapters_from %>
  </div>
  <div class="field">
    <%= f.number_field :chapters_to %>
  </div>
  <div class="field">
    <%= f.number_field :price %>
  </div>
  <div class="field">
    <%= f.text_field :video_url %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

应该就是了!


如果您已经有了表单和现有控制器,请向我们提供更多代码,我很乐意编辑我的答案,为您提供更具体的输入。

到目前为止,这应该足够你使用:)

--- 如果您还需要其他东西,请告诉我们。