Multiple asset directories in Middleman project - assets

I have several microsites, each with its own stylesheet assets, within a larger Middleman project like so:
project/
source/
microsite1.com/
stylesheets/
index.haml
microsite2.com/
stylesheets/
index.haml
stylesheets/
index.haml
config.rb
Now, in production, each microsite is accessed via a domain root, e.g. http://microsite1.com/. But the above directory structure is what's required by my webhost to manage these microsites, so in development it's ideal to access these at http://localhost:4567/microsite1.com/.
However, the paths that asset helpers output aren't relative. For example, in microsite1.com/index.haml:
= stylesheet_link_tag "screen"
yields
<link href="/stylesheets/screen.css" media="screen" rel="stylesheet" type="text/css">
with :relative_assets unset, and yields
<link href="../stylesheets/screen.css" media="screen" rel="stylesheet" type="text/css">
with it set. The former output is correct in the production case; the latter is correct in neither production nor development.
Is there a way to configure Middleman so that I can test at http://localhost:4567/microsite1.com/? Alternatively, is there some way I can simulate http://microsite1.com/? (I thought to try modifying /etc/hosts, though that doesn't seem to work since I'm not pointing at an IP address)

Why do you need to touch the css_dir setting at all? You should be able to use the stylesheet_link_tag helper as follows ...
<%= stylesheet_link_tag "../microsite1.com/stylesheets/microsite1" %>
... within your templates residing in source/microsite1.com. This should give you ...
<link href="/stylesheets/../microsite1.com/stylesheets/microsite1.css" media="screen" rel="stylesheet" type="text/css" />

Here is my hacky but actually pretty functional solution:
# microsite1.com/index.haml
- if development? then $asset_base = "/microsite1.com" end
# config.rb
configure :development do
helpers do
alias_method :original_asset_path, :asset_path
def asset_path(*args)
path = original_asset_path(*args)
if not path =~ ABSOLUTE_URL_PATTERN && defined? $asset_base
path = File.join($asset_base, path)
end
path
end
end
end
tl;dr I'm hooking asset_path to guarantee that all relative assets (stylesheets, javascripts, images) are prefixed with some $asset_base path if specified. (If someone better at Ruby + Middleman than me wants to advise how I might do this without a global variable, I'm all ears.)

Related

Laravel is searching for CSS file in wrong directory when using Elixir

Why is Laravel searching for my CSS file in the wrong directory? I've seen this question posted a few times but it's never actually been answered with Elixir. I am assuming there are possibly some configurations that need to be done which aren't covered in the documentation (at least from what I can tell).
My layout.blade.php file links to the css stylesheet as follows:
<link rel="stylesheet" href="{{ elixir('css/app.css') }}">
My CSS file is loaded in the following path:
localhost/fresh/public/build/css/app-279b78681b.css
However,it is looking for the file here (it is notably missing /Fresh/public/ which I can't determine why?):
localhost/build/css/app-279b78681b.css
I am running an Apache 2.4 webserver for my laravel 5.2 project (not on homestead and not on a VM) and my gulpfile.js file has the following code:
elixir(function(mix) {
mix.sass('app.scss')
.version('/css/app.css');
});
All of that being said, if I were to change my layout.blade.php file to the following:
<link rel="stylesheet" href='css/app.css'>
It will capture my stylesheet that is located in the following directory correctly:
localhost/fresh/public/css/app.css
Sooooo, why can't it capture the css stylesheet when i use Elixir and why is it linking the wrong location?
I have the same problem too.
I used as workaround:
<link rel="stylesheet" href="{{ url(elixir('css/app.css') }}">
or
<link rel="stylesheet" href="{{ asset(elixir('css/app.css')) }}">
It's just that it outputs absolute path then in the generated file. It works but I don't like it so am also still interested how to fix it properly.

Rails asset pipeline - image_path helper only working in development

I have a problem with asset preocompilation in Rails (3.2.7).
I am including an favicon like this:
<link rel="icon" type="image/png" href="<%= image_path("favicon.png") %>" />
On development mode I set config.assets.compile = true. There everything works fine, the rendered HTML looks like this:
<link rel="icon" type="image/png" href="/assets/favicon.png" />
But on production, where I set config.assets.compile = false, I get the error
Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError in Home#index
...
favicon.png isn't precompiled
I already ran rake assets:precompile and i can clearly see, that the asset is available under public/assets/favicon.png.
I know, that i could set config.assets.compile = true in production, but i don´t want to do that (because of performance reasons).
Has anyone an idea, why my rails app is not able to resolve the correct path to the asset in production? Thanks!
Update:
maybe also useful to know: It happens not only for images, also for other assets.
For example <%= stylesheet_link_tag "screen", :media => "all" %> also produces the error screen.css isn't precompiled when config.assets.compile is set to false.
You must tell Rails which assets are to be precompiled. You do this in config/application.rb or config/environments/production.rb with the config.assets.precompile config key.
Rails starts with a default list of assets to precompile, including ["application.js", "application.css"], but if you want your own assets to be precompiled too, you have to add them to the list.
For example:
# config/application.rb
module MyApp
class Application < Rails::Application
# other config ...
config.assets.precompile += ["screen.css", "*.png"]
end
end
Ok, after a couple of tries I figured out, how to fix this. Nevertheless it´s a little bit strange and does not satisfy me completely. It only worked for me, when I set digest to true and provide the path to the manifest:
config.assets.compile = false
config.assets.digest = true
config.assets.manifest = Rails.root.join("public/assets")
It would be interesting to know, what´s behind this "logic".

How can i make Debugging more useful & easier to track bugs on rails development console

When i open the server 'rails s' & then refresh the browser, I get the following error.
Started GET "/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.css" for 127.0.0.1 at 2011-11-27 16:38:06 -0800
ActionController::RoutingError (No route matches [GET] "/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.css"):
I have one single controller & an index action. So obviously the error is coming from there. Here is the sample code from that view.
<head>
<meta charset="UTF-8">
<title>Video Gallery</title>
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<link rel="stylesheet" type="text/css" href="video_gallery.css" />
<link rel="stylesheet" type="text/css" href="jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.css" />
<script type="text/javascript" src="includes/jquery-1.6.1.min.js"></script>
Here is the error -> <script type="text/javascript" src="jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.js"></script>
I know the error is coming coz the path to the file is incorrect.
How can i make the rails console tell me:-
1). From What line number in which particular controller/ View this error is generated?
Any gems for this?
When you install latest ruby with rvm , debugger automatically gets installed. In addition you can add gem to your gemfile # gem 'ruby-debug19', :require => 'ruby-debug'. --- But i still can't run this command rails server –debugger , it gives me error, Also i need the console to point to the particular line in my code from where the error is coming.
Setting consider_all_requests_local to true causes Rails to display those developerfriendly error screens even when the machine making the request is remote. config.consider_all_requests_local = true – user917158 Nov 28 at 1:49
Use the rails s screen for debugging purposes.

rails 3.1 asset pipeline css caching in development

I'm a bit confused as it seems like the application.css is including itself twice, once when it lists the resources from the manifest and then a cache of that. So when I delete an individual file it still seems to stay alive inside the application.css file.
application.css (source)
/*
*= require twitter/bootstrap
*= require_self
*= require_tree ./common
*= require_tree ./helpers
*/
Which works as expected and outputs in dev mode all the relevant individual files
development.rb
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
output
<link href="/assets/twitter/bootstrap.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/application.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/common/announcement.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/common/button.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<Blah blah>
application.css (output)
This should be blank? Since all I have in my application.css file is the manifest and no actual css but instead i get all my concatenated code 106kb long.
IE if I remove a file in the common directory, it doesn't go away. It is no longer listed in the output but the css still appears from the application.css
I had a problem like this before. It was caused after I had precompiled the assets it was going after the applcation.css inside the public folder as well as in the apps directory. I'm not sure how to fix it so that it doesn't keep happening while in dev mode but if you delete your /public/assets directory it should fix it.
Check and see if you have a public/assets folder, if you do and it's full, it's probably why you're seeing double.
There is currently (2012-09-24) a bug in rails/sprockets causing it to not properly detect imported files.
This should be fixed in rails 3.2.9 and later but in the mean time, you can work around it as follows:
Kill the rails instance
rm -rf tmp/cache
Start the rails instance
You should now be seeing the correct css.
You might want to look at
https://stackoverflow.com/a/7854902/686460
"Adding config.serve_static_assets = false to development.rb will prevent loading files from /public/assets"
That did it for me.
#Agustin's solution does it for me but here are a few things you need to do:
Delete everything in /tmp/cache/assets
Add config.serve_static_assets = false to development.rb or test.rb (credits to #Agustin)
Restart your server.
Make sure your application.js / .css is not cached in your browser. Go to http://localhost:3000/assets/application.js?body=1 and hit ctrl+f5 to force refresh (you can also try appending appending a randomizer param at the end: http://localhost:3000/assets/application.js?body=1&rnd=12343
If you get something else and ctrl+f5 still hasn't helped, you need to clear your browser's cache.
Skipping either of these steps returned a cached application.js / .css conflicting with my updates in single files.
The best way, that worked for me is to delete content of tmp/cache/* directory...
The thing that worked for us was setting 'config.assets.debug = false'
This no longer set the HTML included CSS as href="/assets/bootstrap-new.css?body=1", instead set it up as href="/assets/bootstrap-new.css", which I think was the issue.
I had the same problem. Despite clearing tmp/cache and public/assets the minified application.css was still cached and served up from somewhere and my changes to individual css files were not getting served.
This worked for me:
in application.css remove the line *= require_self
restart server
that seems to remove the cache and if you click on application.css in your browser source you will no longer see the minified version
replace the *= require_self back into the file and continue developing.
There was one last step required for me, but I got it fixed. Here's what I did:
shut down the rails server
rake assets:clean
rake tmp:clear
restart the rails server
Then, I refreshed my screen in Google Chrome, and IT STILL DID NOT WORK. So, I launched Firefox and voila, it was actually working. This means that Chrome was caching the old files within the browser. So, I then cleared out the browser cache in Chrome, and it worked!
I know this is an old question, but one fix that worked for me is that I had nginx proxying to my development environment and it had a location ~ ^/(assets)/ block in the configuration. Either comment it out, or try restarting nginx to invalidate the cache. If you're developing, you'll likely just want to comment it out entirely.
I lost way too much time troubleshooting this until I remembered that.
The assets do their job better when running the app in production environment then you'll have loading only the application.css with all the files included, and compressed, there to reduce the server request and this application.css with the compressed styles will be cached.
http://guides.rubyonrails.org/asset_pipeline.html

rails assets path

I have in
<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/assets/application.js" type="text/javascript"></script>
but when I go to http://localhost:3000/assets/application.css
Routing Error
No route matches [GET] "/assets/application.css"
P.S. Rails 3.1.0.rc4, ruby 1.8.7
Seems Sprockets / Rails 3.1 were acting up for me w/ ruby 1.9.2-p180 ... updating to Ruby-1.9.2-p290 seemed to stop the issue.
Maybe not related to your issue... but useful for anyone else having that issue using those versions of Rails & Ruby.
I found that I had something similar going on after updating to Rails 3.1 this evening. I was working on a project that didn't use ActiveRecord, so I had a modified my application.rb to exclude it. The line that usually reads require 'rails/all' to only include the parts I needed, like this:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
This list has changed in Rails 3.1 to include Sprockets, the core component to making the asset pipeline work. I got the asset pipeline serving the serving content as expected by adding this line to the bottom of the list:
require "sprockets/railtie"
After restarting, /assets/application.js and other assets began working as expected.
Note: if you have a custom setup like this, be sure to open the railties gem and look at the contents of lib/rails/all.rb which may have changed (as in this case).
Your scripts and styles will be loaded from the public folder. Drop the assets folder under public and you should be good to go.
In the application layout file, if you have
<%= stylesheet_link_tag "/assets/application" %>
which gives
No route matches [GET] "/assets/application.css"
TRY changing it to
<%= stylesheet_link_tag "application" %>
I had to add the following line in application.rb:
config.assets.enabled = true
in bottom of class Application < Rails::Application