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

时间:2015-12-16 08:09:28

标签: ruby-on-rails wistia

我正在制作一个电子学习平台并拥有课程资源,因此我可以动态添加课程。我现在处于需要将视频上传到每个课程的阶段,我决定使用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表之后,如果将其粘贴到“创建课程”操作的表单框中并提交表单,如何使上述代码按预期工作?什么数据类型应该: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 %>

1 个答案:

答案 0 :(得分:1)

在控制器内部,我们必须允许设置新属性video_url。 Rails将自己处理更新/创建:

# 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 field)添加另一个video_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 %>

那应该是它!

如果您已经有一个表格以及现有的控制器,请使用更多代码打我们,我很乐意编辑我的答案,为您提供更具体的输入。

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

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