对于我的 Rails 应用程序,我有一些项目,用户可以通过 Redactor Rails 发布更新。我有一个BLOGPOSTS
为每个项目制作的控制器和模型。现在,他们可以用这个富文本编辑器很好地嵌入视频。但是,我正在探索添加一个选项,以允许用户在我的网站上直接使用 youtube 进行网络摄像头录制,并将其上传到 youtube 并作为项目的 BLOGPOST 发布。博客文章当前保存在 blogupdates 表中,格式为t.text "content"
.
我在这里引用了文档:https ://developers.google.com/youtube/youtube_upload_widget
* *问题:当我添加 youtube 上传小部件时,它会显示网络摄像头。然后我录制,之后,它只是生成上传的视频以供播放。但是有没有办法我可以获取视频 ID 并将其保存为“BLOGPOST” content
,并在录制视频后自动设置嵌入 html?
所以我在每个项目页面中添加了以下内容:
查看/项目/show.html.erb
<script>
// 2. Asynchronously load the Upload Widget and Player API code.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. Define global variables for the widget and the player.
// The function loads the widget after the JavaScript code
// has downloaded and defines event handlers for callback
// notifications related to the widget.
var widget;
var player;
function onYouTubeIframeAPIReady() {
widget = new YT.UploadWidget('widget', {
width: 500,
events: {
'onUploadSuccess': onUploadSuccess,
'onProcessingComplete': onProcessingComplete
}
});
}
// 4. This function is called when a video has been successfully uploaded.
function onUploadSuccess(event) {
alert('Video ID ' + event.data.videoId + ' was uploaded and is currently being processed.');
}
// 5. This function is called when a video has been successfully
// processed.
function onProcessingComplete(event) {
player = new YT.Player('player', {
height: 390,
width: 640,
videoId: event.data.videoId,
events: {}
});
}
</script>
<div class = "container">
<div id="widget"></div>
<div id="player"></div>
</div>
此外,每个页面都显示了 Redactor Rails 表单:
<%= form_for([@project, @project.blogposts.build]) do |f| %>
<div class="field">
<%= f.text_area :content, label: "Blog Posts", :class => "redactor", %>
</div>
<%= f.hidden_field :user_id, :value => current_user.id %>
<div class="actions">
<%= f.submit "Add Blog Post", :class => "btn btn-header" %>
</div>
<% end %>
blogposts_controller.rb
def create
@project = Project.find(params[:project_id])
params[:blogpost][:content] = sanitize_redactor(params[:blogpost][:content])
@blogpost = @project.blogposts.create!(params[:blogpost])
if @blogpost.save
redirect_to blogs_project_path(@project), notice: "Blog entry created."
end
end
架构.rb
create_table "blogposts", :force => true do |t|
t.integer "user_id"
t.integer "project_id"
t.text "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end