计算使用活动存储显示的视频的观看次数
Count number of views of a video displayed using active storage
使用 active storage
我将视频存储在课程模型中;
class Lesson
has_one_attached :video
end
显示此代码;
<div class='feed'>
<%= render partial: 'lesson', collection: @lessons, cached: true %>
</div>
部分是;
<% cache lesson do %>
<div class="video_size">
<video controls class='video_size'>
<source src=<%= rails_blob_path(lesson.video) %> type='video/mp4' />
</video>
</div>
<% end -%>
我想存储每个视频实际播放频率的计数,即按下视频中的播放按钮。我该怎么做?
在我看来,如果你想统计或存储每个视频实际观看的频率,你应该自定义跟踪
ActiveStorage::YourController at 'show' action
像那样:
# In app/controllers/active_storage/base_controller.rb
class ActiveStorage::BaseController < ActionController::Base
before_action :your_implemnt_method!
include ActiveStorage::SetCurrent
protect_from_forgery with: :exception
def your_implemnt_method!
## check what the video you want to increment.
# and increment number of seen in model via direct for queue it to backgroud worker
content.plus_seen
# do something more.
end
end
到couter_cache或增加观众人数,...等等
另一个解决方案是:你想计算点击播放按钮的次数,只需使用 javascript 处理点击事件,以便将 ajax 请求发送到控制器以增加数数。
JS代码句柄玩法
const player = new Plyr('#player');
$('.plyr__control[data-plyr="play"]').click(function() {
if (!player.source){
Rails.ajax({
type: "POST",
url: "/url-to-do-sth",
data: {your_data: data-to-sent},
success: function(repsonse){
},
error: function(repsonse){
console.log(repsonse);
}
})
}
})
在你的控制器中:
def handle-sth
begin
## Do sothing
# and increment number of seen in model via direct for queue it to backgroud worker
content.plus_seen
render json: {data: data-to-sent}
rescue
render :json => { :errors => ["Error with encode or decode."] }, :status => 422
end
end
使用 active storage
我将视频存储在课程模型中;
class Lesson
has_one_attached :video
end
显示此代码;
<div class='feed'>
<%= render partial: 'lesson', collection: @lessons, cached: true %>
</div>
部分是;
<% cache lesson do %>
<div class="video_size">
<video controls class='video_size'>
<source src=<%= rails_blob_path(lesson.video) %> type='video/mp4' />
</video>
</div>
<% end -%>
我想存储每个视频实际播放频率的计数,即按下视频中的播放按钮。我该怎么做?
在我看来,如果你想统计或存储每个视频实际观看的频率,你应该自定义跟踪
ActiveStorage::YourController at 'show' action
像那样:
# In app/controllers/active_storage/base_controller.rb
class ActiveStorage::BaseController < ActionController::Base
before_action :your_implemnt_method!
include ActiveStorage::SetCurrent
protect_from_forgery with: :exception
def your_implemnt_method!
## check what the video you want to increment.
# and increment number of seen in model via direct for queue it to backgroud worker
content.plus_seen
# do something more.
end
end
到couter_cache或增加观众人数,...等等
另一个解决方案是:你想计算点击播放按钮的次数,只需使用 javascript 处理点击事件,以便将 ajax 请求发送到控制器以增加数数。
JS代码句柄玩法
const player = new Plyr('#player');
$('.plyr__control[data-plyr="play"]').click(function() {
if (!player.source){
Rails.ajax({
type: "POST",
url: "/url-to-do-sth",
data: {your_data: data-to-sent},
success: function(repsonse){
},
error: function(repsonse){
console.log(repsonse);
}
})
}
})
在你的控制器中:
def handle-sth
begin
## Do sothing
# and increment number of seen in model via direct for queue it to backgroud worker
content.plus_seen
render json: {data: data-to-sent}
rescue
render :json => { :errors => ["Error with encode or decode."] }, :status => 422
end
end