How do I include a template part in a Ruby file - ruby

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

Related

Check if image file exists using middleman

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

How to use version.rb plugin in Jekyll static site generator

Here is the plugin code...
module Jekyll
module VersionFilter
def versioned_url(input)
"#{input}?#{Time.now.to_i}"
end
end
end
Liquid::Template.register_filter(Jekyll::VersionFilter)
I am trying to cache bust/version control my .css file. I am new to Liquid. I am having trouble figuring out this basic plugin. Any help?
You need to put version.rb into the _plugins/ directory in the root of your Jekyll site. If you don't have a _plugins/ directory, create one.
For usage - it looks like it gives a new filter that you can apply to text - so you'd use it in your templates to filter the references to your CSS files, adding the query string so that they aren't cached - but I'm sure there's more info on that wherever you got the code from.
For what it's worth, breaking caches with querystrings isn't the best solution. It'd probably be better to write a plugin that adds a new string to the actual filename, and then adds that string to the urls where those assets are included in the templates - but that is a bit more complex.
If using an existing plugin for static asset versioning is an option for you, try jekyll-minibundle.
Assuming you keep non-stamped CSS files in _styles (note the _, because you don't want these to be exported to the production site) and want the stamped CSS files to appear in css, do the following:
<link href="{% ministamp _styles/site.css css/site.css %}" rel="stylesheet" media="screen, projection">
That works fine in combination with compass, just make compass export to _styles.

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)

rails 3.1 maintenance page assets

How do I access assets on my maintenance page in rails 3.1 with the asset pipeline enabled?
With the asset pipeline enalbed all assets have a hash in their filename. However, maintenance pages are vanilla HTML, and rails/passenger is being bypassed by the apache config, so there's no way to generate the right asset paths.
I need my application.css and an image file. It's fine if they're the old ones.
I can think of a few kludges, but they're all lame:
On each deploy, symlink the assets I need to a generic name that I use in the maintenance file.
Make my maintenance page dynamic, generate it, and dump the markup somewhere - then modify my maintennce 'deploy' script.
If you want to avoid a symlink or a dynamic (erb) page, then use a static template and modify this during deployment.
First create a maintenance page template.
During deployment read the mainfest.yml file created by the precompile process.
Read in the template of the maint page.
Replace any files named in the template with the hashed version from the manifest.
Write out the altered template to the file-system.
For anyone's convenience, I adopted Richard's solution in my project and created a simple ruby script that replaces asset links in the static HTML error / maintenance pages. It is deliberatery NOT a rake task so that it is as fast as possible. It has no Rails depenency anyway, other than that it has to be run from the rails root directory.
#!/usr/bin/env ruby
require 'yaml'
GLOBS = %w(public/errors_source/*.html)
MANIFEST = "public/assets/manifest.yml"
manifest = YAML::load(File.open(MANIFEST))
GLOBS.each do |glob|
Dir.glob(glob).each do |file|
next unless File.file?(file)
contents = File.read(file)
manifest.each do |asset, compiled_asset|
contents.gsub!(asset, "/assets/#{compiled_asset}")
end
File.open(file.gsub('errors_source/',''), 'w') do |outfile|
outfile.write(contents)
end
end
end
The script expects the static HTML error/maintenance pages to live under the errors_source directory and copies them (with assets replaced for their hashed versions) to the rails root directory.
A sample maintenance page then may look like this (notice the CSS asset link and logo image - these assets are simply shared with the main rails code):
<html>
<head>
...
<link href="application.css" media="screen" rel="stylesheet" type="text/css"/>
</head>
<body>
...
<img src="logo.png" width="161" height="61"/>
...
</body>
</html>
If you are on Heroku, there's an add-on called Trackman.
You can link all your assets and it deploys your pages and assets on S3.
You practically have nothing to code.
You can use the utilities within the gem to also make the dev a piece of cake.
http://www.trackman-addon.com

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