Using Panda (pandastream) Video to show all videos in Index Action - ruby-on-rails

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.

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.

Related

How to make a Facebook pixel fire on a rails controller action

I have a ruby on rails app. I followed the facebook instructions to add a pixel.
However, to track a conversion, facebook requires you to put the page in the conversion that will appear when the desired result is reached. i.e if I want to show a customer has signed up, I would put the page you go to after signing up as the success object to track.
My issue is that in my application when a customer signs up, there is no landing page. The app takes the user back to the homepage. It shows a message on the homepage so I am trying to see if there is a way to track conversions from controller actions instead of actual pages.
The actions I need to count don't have pages, they are controller actions. Is there a gem, documentation, or a best practice on how to do this that anyone is aware of?
Here is the pixel that goes into the layouts file...
<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '18027724*****', {
em: 'insert_email_variable,'
});
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=180277241*****&ev=PageView&noscript=1"
/></noscript>
<!-- DO NOT MODIFY -->
<!-- End Facebook Pixel Code -->
However I need it to fire here in the controller...
def create
#reportapproval = current_user.reportapprovals.create(authorization_params)
if #reportapproval.valid?
if #reportapproval.manager_paying_report == true
flash[:notice] = "Please make payment before proceeding"
redirect_to new_managers_charge_path(id: #reportapproval.id)
else
flash[:notice] = "You have requested a report from #{#reportapproval.tenant_last_name}."
redirect_to managers_dashboard_path
#reportapproval.save
end
<----PIXEL FIRES HERE IN THE PROCESS ----->
else
flash[:error] = #reportapproval.errors.full_messages.to_sentence
redirect_to managers_dashboard_path
end
end
A cleaner solution might be to use the rack-tracker gem found here:
https://github.com/railslove/rack-tracker
The master-branch doesn't yet have the new facebook pixel, so use this fork instead:
https://github.com/jolim/rack-tracker
Add this to your gemfile:
gem 'rack-tracker', git: 'https://github.com/jolim/rack-tracker.git', ref: 'c2b1b30'
In your application.rb:
config.middleware.use(Rack::Tracker) do
handler :facebook, { id: 'PIXEL_ID' }
end
Where you replace PIXEL_ID with your facebook pixel id.
Then in your controller:
def create
...
# PIXEL FIRES HERE IN THE PROCESS
tracker do |t|
t.facebook :track, { type: 'Purchase', options: { value: 100, currency: 'USD' } }
end
...
end
You can add a helper to check the current controller and action, then wrap displaying the tracking pixel in that. It would look something like:
# In ApplicationHelper
def display_tracking_pixel?(controller)
controller.current_controller == "my_controller" && controller.current_action == "my_action"
end
# In the layout
<% if display_tracking_pixel?(controller) %>
<!-- Tracking embed goes here -->
<% end %>

Is there any complication of using IFrames in Rails?

I am trying to preview a PDF in my rails application. The file sits in the uploads folder and not in the public or assets folder. So I have a action in my controller called preview which uses send_file.
I have a before filter for preview called check_shared which checks if the user is allowed to view the file or not.
When this entire workflow works for a simple image preview where I just use an image_tag to render the image, it fails to work with an IFrame. An error arises in the check_shared private action which says that "current_user"( I am using Devise) is not a valid method.
Does the controller action somehow lose it's scope in the Iframe?
Here is the view:
<%= image_tag preview_myfile_path(#myfile) %>
<iframe src="/pdfjs/web/viewer.html?file=<%= preview_myfile_path(#myfile) %>" id="pdf_frame" style="border: 0" width="100%" height="1000" frameborder="0" scrolling="no"></iframe>
Here is the preview action:
def preview
# Introducing choosepath, to reduce double render errors.
send_file choosepath(#myfile)
end
Here is the before action:
def check_shared
puts "inside check_shared"
#myfile = Myfile.find(params[:id])
if current_user.id == #myfile.user_id
puts "owner!"
else
flash[:notice] = "You do not have permission to access this file!"
redirect_to root_path
end
end

Rails - ability to link directly to views that are rendered via ajax

Looking for the best way to implement this. Currently I have a "show" page for Users - that shows all of a users' pictures.
def show
#user = User.find(params[:id])
#pictrues = #user.pictures
end
On that page, I have various tabs. When a user clicks on one of those tabs, an ajax call renders a view... particularly, it updates a partial that was previously showing all of a users pictures (and an additional partial for statistics). For the "show comments" example, it updates the partial with all of the pictures a user has commented on:
def show_comments
#user = User.find(params[:id])
#pictures = #user.picture_comments.map{ |p| p.picture }.uniq
respond_to do |format|
format.html
format.js
end
end
The show_comments.js.erb file looks like:
$("#user_content_container").html("<%= escape_javascript render(partial: 'shared/pictures', pictures: #pictures) %>");
$("div.user_header").html("<h4>Comments</h4><br/>");
$("#stat_container").html("<%= escape_javascript render(partial: 'shared/comment_stats', pictures: #pictures) %>");
What I want to do, is to keep the current functionality of the page. But also be able to link directly to the views that are rendered via ajax. For example, have a link on another page that goes directly to the users "show" page, as it is when the "comments" tab is clicked on.
I have a few ideas, but an not sure what the "cleanest" way of doing this would be. Let me know if you need any additional clarification, b/c I'm honestly having as difficult time wording this question, as I am in finding the best way to implement this!
This sounds like something that you might be able to solve using turbolinks. If you can update your app to use Rails 4, you get this bundled in. Otherwise, you can use the gem. For more information on how to do this, watch the railscast on turbolinks.
If you don't want to or can't use them, you could also try passing params in the url, and check for them when the page is loaded. You could use the params to modify the page in the same way that it would be modified after the AJAX call.

How to call a ruby on rails controller when view is loaded?

I am pretty new to Rails, basically what I want to do is pretty simple, when the view is loaded I want it to call a controller that executes a ruby script and render the image generated by this script on my view, is this possible? Or should I call the script before the view is loaded and just retrieve the generated image?
First of all what i would suggest you to go and learn here a link for your help
ruby on rails tutorial
Answer to your problem
In rails controllers are loaded first, each view is associated to some controller action, forexample if you have users controller(app/controllers/users_controller.rb) and in show you want to display the users image as a profile picture, what you can do is
def show
#user = User.find(params[:id])
end
and in its corresponding view(app/views/show.html.erb)
<img src = <%= #user.image_path %> />
I would retrieve the image on load of the page, hide it with css and load it anytime you want with javascript. I don't see a big reason for not calling it on the load of the page.
In RoR a controller action is rendering the view, so you can set it at the initial load itself.
For example, you have the Users controller and you need to show all users images in the index view.
controllers/users_controller.rb
def index
#users = User.all
end
views/users/index.hrml.erb
<% #users.each do |user| %>
<img src=<%= user.image_path %>> </img>
<% end %>

show flow player with using AJAX

I am using flow player in my site to view the videos , Now on the index view i have all the videos . but to show that video i have made remote_link to open it on the same page
I got the video on the page too , but it is not displaying , i can even save the video by clicking it , but i cant see the player at all my code is
controller
#video = Upload.find_by_id(params[:id])
render :update do |page|
page[:"div_video_#{#video.id}"].innerHTML = render :partial => "display_video"
page[:"div_video_#{#video.id}"].show
end
Waht i want is to dispaly the video by ajax call from controller , please help me?
The problem is that the flowplayer function has already been initialized on the page, and so won't affect any new html you put into it, you'll need to recall the flowplayer function after the page has been updated, something like this:
#video = Upload.find_by_id(params[:id])
render :update do |page|
page[:"div_video_#{#video.id}"].innerHTML = render :partial => "display_video"
page.flowplayer("player", "path/to/the/flowplayer-3.1.5.swf")
page[:"div_video_#{#video.id}"].show
end

Resources