23

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...

delta2006
  • 425
  • 2
  • 4
  • 13

2 Answers2

45

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.

Pedro Cori
  • 2,046
  • 2
  • 16
  • 23
  • This provides a working solution, but the solution below is the correct one. – Noah Clark Nov 16 '11 at 21:41
  • @NoahClark Yup. Wasn't aware of the video_tag. Rails never ceases to surprise me :) – Pedro Cori Nov 16 '11 at 22:25
  • I tried BOTH solutions. I am still getting No route matches [GET] "/assets/media/truffle1.mp4" – delta2006 Nov 17 '11 at 01:30
  • I tried @Pragnesh Vaghela 's answer, played with it for a bit, looked at the [documentation](http://guides.rubyonrails.org/asset_pipeline.html) for a bit more, and got it to work. Posted on the edit. – Pedro Cori Nov 17 '11 at 04:35
  • Not sure if it matters, but to add paths to assets, the rails guides uses the .join method instead of hard-coded slashes: config.assets.paths << Rails.root.join("app", "assets", "videos") – autodidakto Jan 07 '12 at 23:41
  • I was able to link to the video but it remains black. – balexander Jul 25 '13 at 02:29
  • Hello! How I can add speed controls to player? It s possible? – Sinigr Aug 13 '18 at 13:36
8

You can use the video_tag helper which builds an HTML 5 tag. Video files are loaded from 'public/videos' by default.

<%= video_tag "truffle1.mp4", :size => "320x240", :controls => true %>
Pragnesh Vaghela
  • 1,327
  • 12
  • 16