Image assets not found after upgrading to Rails 3.1 - ruby-on-rails-3.1

I've upgraded an app to Rails 3.1, and everything is setup and working correctly except that an image_tag isn't finding the image. I've moved all my images from public/images to app/assets/images. The browser requests an image at http://localhost:3000/assets/foobar.png but it just shows up as a broken image in Chrome. Opening it in a separate tab says "No route matches [GET] "/assets/foobar.png".
I feel like I'm just missing some setting somewhere, but I haven't been able to find out what it is yet. Any suggestions?

One of the biggest things they don't mention in the Assets Pipeline Guide is to remove the leading /images/ part of the path. So for example this:
<%= image_tag "/images/about/header.png" %>
Would have to be changed to this:
<%= image_tag "about/header.png" %>
Same goes for CSS files. So this:
background-image: url("/images/alliance/header_background.png");
Would change to:
background-image: image-url("alliance/header_background.png");
Note that when using the asset pipeline never include the leading slash. Also if you use the above line of code in your CSS file, be sure to add .scss to the file (so application.css would become application.css.scss).
Hope this saves you some headache!

Related

Duplicated /blog link at pagination section

I new to octopress and I passed thought an issue with main page links.
All links was duplicated like /blog/blog and I fixed this at source files.
Now, everything is fine but "Blog Archives" link (/blog/blog/archives) at div with class "pagination", at index.html, close to footer section.
I looked into source files and it appears fine but if I run "rake generate", after change the wrong url at index.html, it backs to /blog/blog/archives.
Which file I need to change to fix that issue?
The header "Blog Archives" is fine.
I solved changing permalink: /blog/:year/:month/:day/:title/ for permalink: /:year/:month/:day/:title/ in _config.yml, like hsigrist said in the comment.

URI mismatch Rails generates MD5 digest in asset URL in production, but controller GET's a non-MD5 URL

I have a Rails app with a page or two that uses the html5 video tag, and the corresponding Rails video_tag() method to display video on the page.
I've added
<%= video_tag "car_circle.mp4", width: "640", height: "500", controls: true %>
to an html.erb file in views.
Rails 3.2 looks for videos in the /public directory, and so I've added my videos to a folder called public/videos.
In order to add this folder to the asset pipeline, I added this line to application.rb
config.assets.paths << "#{Rails.root}/public/videos"
This partially works. The video assets do precompile, and the pages with the videos in them do in fact load. However, the videos themselves do not. This is due to the fact that, while the videos can be viewed at their respective URLs, for example /videos/car_circle.mp4, the URI of the video on the page where the videos should load is /videos/car_circle-4e5f6739c10827a6fc3d479d1ac5c275.mp4.
This is the MD5 digest Rails adds on to URI's in production. I need my video URI to match the digest'ed URI on the page where the video resides.
So far I have tried to disable digesting by setting
config.assets.digest = true
to false. However this is not recommended and it makes my app crash anyhow.
Any suggestions on how to get my asset GET'ed correctly? Obviously this is only a problem in production, which is the only place where Rails uses digest'ed URI's by default. Much appreciated.
Try to use plain html element declaration as follows:
<video width="640" height="500" control="controls">
<source src="../../../public/videos/car_circle.mp4" type="video/mp4">
</video>
Even .erb file will still accept plain html declaration that should be free from any Rails' alteration.
Using plain html for the video tag also worked for me.
I kept the video files in public/videos
and referenced the html as
<video autoplay="autoplay" loop="loop" src="/videos/test.mp4">
This worked fine for me.

Rails 3.x asset pipeline including coffeescript file in view

I have recently came across where I would like to simply just include a coffeescript file in a single Rails view, but when you use the javascript_include_tag it appends .js to the end of the name you pass in (for obvious reasons).
Has anyone come up with a solution to simply just include a coffeescript file on a page without this happening?
i did this to load only the relevant JS for each controller:
in application.html.erb where you see
<%= javascript_include_tag "application" %>
please add
<%= javascript_include_tag controller_path, :media => "all" %>
also remove the
#=import .
from application.js
this way only the myController.coffee.js is loaded from myController this way you can have a more granular control of the loaded js and you can use application.js for common code
it's not the best solution in terms of user download but if you don't care about a few more connections I find this easier to understand (same thing can be done with css)

Spring web development images

Can anybody tell me or give me a link to go to which can tell me how to implement and display images step by step (I'm only beginning) on a webpage from a spring project
I'm using IntelliJ
Thanks
What's the URL of the page? The one that appears in the location bar of your browser?
That is the URL to which relative locations are resolved in the HTML code. So, if the URL is http://localhost/MyApp/foo.html, and the URL of the CSS inside the HTML code is ../../css/style.css, the absolute URL where the browse will try to find the CSS will be http://localhost/MyApp/../../css/style.css, which doesn't make sense.
I prefer always using absolute paths for images and CSS files (and other resources). Using JSTL, that makes it like
<link href="<c:url value='/css/style.css'/>" ...
The <c:url> tag takes care of prepending the application context (/MyApp) to the path.
Note that relative paths inside CSS files are not resolved relative to the page URL, but relative to the location of the CSS file itself. So the path in your CSS file is correct.

Rails 3.1 assets and layout troubles

I have a rails 3.1 application that uses a default layout with css in "/app/assets/stylesheets/application.css" as per standard practice. This works great with the asset pipelining stuff for the main part of my application.
But I use a different "reporting" layout when I generate a report that uses a /app/views/layouts/showreports.html.erb layout as follows:
<!DOCTYPE html>
<html>
<head>
<%= stylesheet_link_tag "showreports" %>
<%= javascript_include_tag "showreports" %>
<%= csrf_meta_tag %>
</head>
<body>
<div id="mainarea" class="container">
<div class="span-24 last">
<%= yield %>
</div>
</div>
</body>
I'm using rake assets:clean; rake assets:precompile to pre-compile my assets and check things are looking good in the manifest file (/public/assets/manifest.yml) and I don't see any references to showreports.css or showreports.js.
When I test my program in dev-mode, unsurprisingly, it can't locate those files.
I'm guessing this is a basic sprockets question with rails 3.1 but I thought sprockets would be smart enough to parse all the layout files for assets (beyond the default application.html.erb file).
Just wondering if this may be a bug or just my misunderstanding on how this should work.
Cheers,
Greg
UPDATE
After more tinkering, I'm answering my own question as this made the most sense for me...
This answer really gave the right clue
What I was doing wrong was using files named "showreports.css" and "showreports.js" as manifest files.
My fix was to create /app/assets/stylesheets/showreports and /app/assets/javascripts/showreports and then to rename my showreports.(css|js) to application.(css|js) inside their respective directories.
After doing so, rake assets:precompile found them nicely and added them correctly to the manifest file.
According to http://guides.rubyonrails.org/asset_pipeline.html you should also add following:
config.assets.precompile += %w(showreports.css showreports.js)
to your application.rb file
It's a misunderstanding. Rails Assets Pipeline only compiles files located in RAILS_ROOT/app/assets and you must reference them directly. If you want another file to be generated simply create another manifest file with similar content to application.css/js. Also check require directives to avoid including one file into the other.

Resources