Using Panda (pandastream) Video to show all videos in Index Action

241 Views Asked by At

I'm using PandaVideo (http://www.pandastream.com/docs/integrate_with_rails) to upload videos in my Rails app. I'm having trouble taking the code from the docs at Panda and Heroku to relate it to the index action to show ALL of the videos, both on the Video's controller index action and on the User's profile to show each user's videos.

Here is the code that they give to find and show the video on the Video's SHOW action:

@video = Video.find(params[:id])
@original_video = @video.panda_video
@h264_encoding = @original_video.encodings["h264"]

then on the show view, I reference the video based on the last variable @h264_encoding

This works nicely. Now, I need to somehow take this code and use it to show all videos on a single page. For this example, let's show all of a particular user's videos on their page.

def show
  @user = User.find_by(username: params[:username])
  # not sure what goes here to find that user's videos (from Panda).
  # If i were just using paperclip for instance, I could easily write:
    @videos = @user.videos # but I need to use the Panda (the @h264_encoding variable) to find the video.
end

maybe this is useful...here is part of the video model

def panda_video
  @panda_video ||= Panda::Video.find(panda_video_id)
end

I hope I've provided enough code. If not please let me know and I'll add more. Again, I'm trying to show all of a particular user's videos from PandaStream.

1

There are 1 best solutions below

1
On BEST ANSWER

Not sure if I'm missing something, but why wouldn't it be as simple as this:

def index
  @videos ||= Video.all
end

As far as your show, I see nothing wrong with this:

def show
  @user = User.find_by(username: params[:username])
  @videos = @user.videos
end

Then in your view something like the following:

<%= @videos.each do |video| %>
  <% h264_encoding = video.panda_video.encodings["h264"] %>
  <video id="movie" width="<%= h264_encoding.width %>" height="<%= h264_encoding.height %>" preload="none" 
    poster="<%= h264_encoding.screenshots.first %>" controls>
    <source src="<%= h264_encoding.url %>" type="video/mp4">
  </video>
<% end %>

You are simply referencing Panda's API to retrieve the information relating to an upload, however you handle all the relationships and the User and Video models.

Let me know if I'm missing something, as it seems you are on the right track.