Deleting empty asset files in Rails - ruby-on-rails

When using scaffolding in Rails, you end up with placeholder .css/.scss and .js/.coffee files under the assets directories. Unless I am adding something to one they will stay empty.
I would assume the assets pipeline would ignore the empty files when compiling, but is there any reason I wouldn't just delete them all?

rails generate scaffold is just a helper. It simply generated everything that could possibly be useful to you.
Sure you can delete all unneeded files. You could also delete a model or controller after it has been created if you don't need it anymore.

Related

How to preprocess files in the "public" folder in Rails?

I've got a javascript file that I need to be accessed from outside my site (from an external site), but I also want to pre-process the file (ie. with Rail's Asset pipeline) so I can make use of some environment variables in the JS File.
Any idea how to do this? If I put the JS file into the public folder, I cannot pre-process it. However, if I put the file into the assets folder structure I can pre-process it, however, it is then not public.
Please excuse my ignorance, I'm new to this! ;)
Cheers,
Matt.
What you put in your assets folder is indeed public. If it weren't you could not link to javascripts, stylesheets and images there. The only problem is that rails hashes the name of the files for cache busting purposes (only in production).
As a workaround you may create a controller action that gets the hashed file path using the javascript_url helper and redirects to it.
class ThisController < ApplicationController
def some_action
redirect_to ActionController::Base.helpers.javascript_url('javascripts/your_file')
end
end
Maybe try using grunt to build a task that exports the processed javascript file to the public folder and overwrites the old version everytime it builds.
http://developers.mobilesystem7.com/blog/post/replacing-rails-asset-pipeline-with-grunt-bower-browserify/

Twitter Bootstrap in Rails

I generated views using this command
rails g bootstrap:themed Todo
it generated several views for my model.
I am using git and reverted the project to its former branch without the boostrap.
Now, I am trying to regenerate the views but it always returns this
identical app/views/manifests/index.html.erb
How do I force the bootstap generator to regenerate the views for my project?
TIA
I was able to work it out by moving my project into another directory, preferably into a different folder.

Rails - how do you edit files included by a gem in the asset pipeline?

I am new to rails so this is probably a simple question about using the asset pipeline.
In my app, I want to use this jquery plugin: http://www.fyneworks.com/jquery/star-rating/
So to do it, I included the following gem in my gemfile: https://github.com/RichGuk/jquery-star-rating-rails
However, I find that the image used for the star ratings is too low resolution and I'd also like to change the style. However, all 3 versions of the stars that are displayed are held in one image so I'd have to play around with the scripts as well to make sure they are configured properly if I make the image for the stars larger.
Back to my question: How do I edit this image file in my application?
I've tried downloading all the files and putting them in my vender directory and editing the file but it did not seem to work.
I know the files are included by the gem but how do make the files visible to edit?
Appreciate the help!
So the asset pipeline consists of potentially many directories (assuming you are using gems that inject their own assets into the pipeline). When an asset is being grabbed in Rails, Rails goes through these directories (in the same order, every time) to find the asset. When the name of the file is first found, that's it, Rails grabs it and uses that file.
Vendor asset directories are specified after app assets, I believe. So, if you place the image that you want to change in the app/assets/images folder, you'll essentially be overriding that vendor image in your application with your own image since Rails will search it's own app/assets first. Obviously, the files need to be named the same.
Try adding your star image in your assets path. It seem to
reference star.gif using the asset_path
I would also try
overriding the star plugin by creating your own css file.

Rails 3.1 and Image Assets

I have put all my images for my admin theme in the assets folder within a folder called admin. Then I link to it like normal ie.
# Ruby
image_tag "admin/file.jpg" .....
#CSS
.logo{ background:url('/assets/images/admin/logo.png');
FYI. Just for testing I am not using the asset_path tag just yet as I have not compiled my assets.
Ok all good so far until I decided to update an image. I replaced some colors but on reload the new styled image is not showing. If I view the image directly in the browser its still showing the old image. Going one step further I destroyed the admin images folder. But it has broken nothing all the images are still being displayed. And yes I have cleared my cache and have tried on multiple browsers.
Is there some sort of image caching going on? This is just local development using pow to serve the pages.
Even destroying the whole images folder the images are still being served.
Am I missing something?
In 3.1 you just get rid of the 'images' part of the path. So an image that lives in /assets/images/example.png will actually be accessible in a get request at this url - /assets/example.png
Because the assets/images folder gets generated along with a new 3.1 app, this is the convention that they probably want you to follow. I think that's where image_tag will look for it, but I haven't tested that yet.
Also, during the RailsConf keynote, I remember D2h saying the the public folder should not have much in it anymore, mostly just error pages and a favicon.
You'll want to change the extension of your css file from .css.scss to .css.scss.erb and do:
background-image:url(<%=asset_path "admin/logo.png"%>);
You may need to do a "hard refresh" to see changes. CMD+SHIFT+R on OSX browsers.
In production, make sure
rm -rf public/assets
bundle exec rake assets:precompile RAILS_ENV=production
happens upon deployment.
For what it's worth, when I did this I found that no folder should be include in the path in the css file. For instance if I have app/assets/images/example.png, and I put this in my css file...
div.example { background: url('example.png'); }
... then somehow it magically works. I figured this out by running the rake assets:precompile task, which just sucks everything out of all your load paths and dumps it in a junk drawer folder: public/assets. That's ironic, IMO...
In any case this means you don't need to put any folder paths, everything in your assets folders will all end up living in one huge directory. How this system resolves file name conflicts is unclear, you may need to be careful about that.
Kind of frustrating there aren't better docs out there for this big of a change.
In rails 4 you can now use a css and sass helper image-url:
div.logo {background-image: image-url("logo.png");}
If your background images aren't showing up consider looking at how you're referencing them in your stylesheets.
when referencing images in CSS or in an IMG tag, use image-name.jpg
while the image is really located under ./assets/images/image-name.jpg
http://railscasts.com/episodes/279-understanding-the-asset-pipeline
This railscast (Rails Tutorial video on asset pipeline) helps a lot to explain the paths in assets pipeline as well. I found it pretty useful, and actually watched it a few times.
The solution I chose is #Lee McAlilly's above, but this railscast helped me to understand why it works. Hope it helps!
The asset pipeline in rails offers a method for this exact thing.
You simply add image_path('image filename') to your css or scss file and rails takes care of everything. For example:
.logo{ background:url(image_path('admin/logo.png'));
(note that it works just like in a .erb view, and you don't use "/assets" or "/assets/images" in the path)
Rails also offers other helper methods, and there's another answer here: How do I use reference images in Sass when using Rails 3.1?

Put files (css, html, js, images) per feature in one place in Rails 3?

Whenever I add a new feature (eg. something I downloaded) I tend to want to put all the files (css, html, js, images) in one place.
Symfony 2.0 will have this new feature they called bundle system. Everything will be in its own folder. This will be great for adding new features so you don't have to mix all css, js, image files with each other. It should be per feature instead.
And also it would be great for deleting features. Then you know that all files are in one place and don't have to look for them throughout your application.
Eg.
Instead of this...
images/
fader.img
cart1.img
cart2.img
javascripts/
fader.js
cart.js
stylesheets/
fader.css
cart_main.css
cart_sub.css
...you should have it like this...
venture/
fader/
fader.img
fader.css
fader.js
cart/
cart1.img
cart2.img
cart.js
cart_main.css
cart_sub.css
Is there a way of doing so in Rails 3?
Sure, you could just treat them like a plugin - making a set of files into a plugin is very simple, after all - you basically just put them in a folder, in a file structure parallel to the root of your rails app, then put that folder in your vendor/plugins folder.
Here's the guide on it: http://guides.rubyonrails.org/plugins.html
Then, if you want to delete a feature, just destroy it's plugin folder, and you're clean.

Resources