Check if image file exists using middleman - ruby

I have a website running on middleman. I'm trying to check if an image file exists. I followed the suggestion in "How to check for file existence", using File.file?, but it's not working. Here's the line of code:
<% if File.exists?("/source/images/doctor-#{doctor.slug}.jpg") %>
I tried a few different file paths:
/source/images/file
/images/file
../images/file
but none worked. In the browser, I can view the image file at "0.0.0.0:4567/images/file".
Any ideas?

Maybe it's easier to query those files via the Middleman Sitemap?
You gain access to it within templates via the sitemap object. One way of achieving your actual case could be:
<% if sitemap.find_resource_by_path("images/doctor-#{doctor.slug}.jpg") %>
<%# do what you want %>
<% end %>

Related

How do I include a template part in a Ruby file

I'm working with a ruby website app and I'm not familiar with Ruby , I would like to know how you include say a nav template file in a html file, I'm familiar with how you would do this in PHP with require
So this would include the nav section on each page , what is the code to do this in Ruby
Many thanks
If you're using erb to generate your html, you have to add to the corresponding .erb file (in /app/views/*/):
<%= render 'nav' %>
And then the file *_nav.html.erb* will be included at that point (from the same dir where the includer file is).

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)

Favicon with Rails 3.1 not showing up? [duplicate]

This question already has answers here:
Adding icon to rails application
(5 answers)
Closed 9 years ago.
I can't get my favicon to show up. It's called favicon.ico and inside of public directory (folder). My development log shows no problems with the favicon. I put the link in my application layout:
<!DOCTYPE html>
<html>
<head>
<%= csrf_meta_tag %>
<%= favicon_link_tag "/favicon.ico" %>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
</head>
<body>
<%= yield %>
</body>
</html>
But its still not showing up in Firefox. I cleared my cache with Firefox and I also tried <%= favicon_link_tag %> too. I haven't tried production but can you even see it in localhost? What am I missing?
UPDATE
So its showing up in Chrome but not Firefox. Any idea why?
If you suspect caching is the problem, you could also trick the browser into loading the new icon by adding a parameter to the filepath.
Instead of
favicon_link_tag "/favicon.ico"
Use
favicon_link_tag "/favicon.ico?1"
Clear the cache again in Firefox (I usually just clear everything) and then check out the network traffic when you load your page. Make sure that favicon.ico is being requested. If not, then it's probably being cached somewhere. I had lots of issues with this the other day but after a couple of cache clears it suddenly started working.

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.

Image assets not found after upgrading to 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!

Resources