using HTML5 video tag in a simple rails app

delta2006 picture delta2006 · Nov 16, 2011 · Viewed 24.3k times · Source

inside index.html.erb there is the following code

<video width="320" height="240" controls="controls">
      <source src="truffle1.mp4"/>
 Your browser does not support the video tag.
</video>

I am not sure where to put my mp4 video file so I put it at several places.
Next I fire up the rails server and use Chrome to open the index page. I see the black video frame but it does not play. and when I try open video in a new window. I get No route matches [GET] "/admin/truffle1.mp4" (note admin is just the namespace for the controller).


seems like this is a rails routing problem...

Answer

Pedro Cori picture Pedro Cori · Nov 16, 2011

When you say src="truffle1.mp4"you're telling Rails to look for that file from the current route (you're probably on localhost:3000/admin if you're trying it on a local server, so it's looking in localhost:3000/admin/truffle1.mp4).

You could try giving it the route from the home of your app like so: src="/assets/media/truffle1.mp4", and put the file in that directory (you'll probably have to create it).

EDIT

Following the answer provided by @Pragnesh Vaghela, I managed to make it work. Your first intuition was right. You're missing routing if you want to have your videos in /assets/videos. When you say:

<%= video_tag "truffle1.mp4", :size => "320x240", :controls => true %>

the server will look for the file in all the assets directories that have been routed (by default: stylesheets, images, and javascripts). If you put your video in images, it should work, for example. If you want to have the /assets/videos directory searched also, you have to add the following line to your config/application.rb file:

config.assets.paths << "#{Rails.root}/app/assets/videos"

You can put it under the line that says:

config.assets.enabled = true

I believe.

Hope this works.