Rails 3.1 assets and layout troubles - ruby-on-rails-3.1

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.

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

Asset Pipeline in a sinatra app

I have been trying to follow a guide to setting up the asset pipeline in a sinatra app, the aim to reduce my http requests when loading my web page.The guide is Located Here
I think im stumbling on creating a module within a sinatra app, apologies if this is basic but havent done this before.
So i created a folder called modules and placed an assets.rb file within it. I have modified the script to suit my needs
class Assets < Sinatra::Base
configure do
set :assets, (Sprockets::Environment.new { |env|
env.append_path(settings.root + "/assets/js")
env.append_path(settings.root + "/assets/css")
# compress everything in production
if ENV["RACK_ENV"] == "development || production"
env.js_compressor = YUI::JavaScriptCompressor.new
env.css_compressor = YUI::CssCompressor.new
end
})
end
get "/assets/js/app.js" do
content_type("application/javascript")
settings.assets["app.js"]
end
get "/assets/css/app.css" do
content_type("text/css")
settings.assets["app.css"]
end
end
my assets directory structure
assets
css
app.css
other.css
js
app.js
other.js
my config.ru
require './david'
use Assets
run Sinatra::Application
In each of my app.js and app.css i have put
// require_tree
but in my js file it is greyed out and in the css it is still in white?
I have installed both gems required but still when loading the page there are multiple http requests rather than grouping all the css and js as one call
Can anyone see anything im missing, mainly the module setup im guessing?
Thanks
EDIT
My current layout file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>David's Carpets</title>
<!--stylesheets-->
<%= stylesheet_link_tag "/assets/css/font-awesome.min.css" %>
<!--[if IE 7]>
<link rel="stylesheet" href="/assets/css/font-awesome-ie7.min.css">
<![endif]-->
<%= stylesheet_link_tag "/assets/css/bootstrap.min.css" %>
<%= stylesheet_link_tag "/assets/css/bootstrap-responsive.min.css" %>
<%= stylesheet_link_tag "/assets/css/custom.min.css" %>
<%= stylesheet_link_tag "/assets/css/resp-980.min.css" %>
</head>
<body onload="initialize()">
<%= styled_flash %>
<%= yield %>
<%= erb :footer %>
<!-- Javascript -->
<script src="/assets/js/jquery-1.7-min.js" type="text/javascript"></script>
<script src="/assets/js/valid_mail.min.js" type="text/javascript"></script>
<script src="/assets/js/twitter.min.js" type="text/javascript"></script>
<script src="/assets/js/custom.min.js" type="text/javascript"></script>
</body>
</html>
I think you'd be better to add the root path using a proc:
set :assets, Proc.new { Sprockets::Environment.new(root) {|env|
env.append_path File.join( root, "/assets/js")
env.append_path File.join(root, "/assets/css")
# moreā€¦
}}
See if that improves things. You might want to use Pry or just warn to check that the value of settings.assets["app.js"] is what you expect it to be.
Workarounds
Like I said in the comments above, things don't always map well from one framework to another. Personally, I precompile my assets using Guard/SASS and Guard/Coffeescript into the public folder. There are also minification libraries that hook up with Guard I then use Sinatra Static Assets or Sinatra Exstatic* to point to the files in views/layouts. I don't like to combine the javascript into one file (YMMV).
I also wanted the jQuery stuff that Rails added in via jquery-rails, so I wrote rack-jquery, rack-jquery_ui, and rack-jquery_ui-themes. They may be of interest to you.
Another way to get Sprockets working for you would be to use Rack. I found this blog post that shows you how:
http://metabates.com/2011/08/31/using-sprockets-without-rails/
I also wrote Sinatra Exstatic, it's a fork of Sinatra Static Assets. It's a recent fork, if you use it any feedback will be welcome :)
Additional, now that the templates are posted in the question:
Sinatra won't do anything magic for you to point to the "super" css/js file, so if you have several CSS and javascript links then a client will still make several requests to the individual files. One way around this would be (in the case of the JS) to only have one statement, e.g:
<!-- Javascript -->
<script src="/assets/js/app.js" type="text/javascript"></script>
and that's it. Another way to do this would be to keep all the statements you've got, but catch every statement using the route, e.g:
# The local variable `name` isn't used below
# but just in case you ever need it, it's there
get "/assets/js/:name.js" do |name|
content_type :javascript
settings.assets["app.js"]
end

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.

Resources