how to have assets other than js and css files in rails - ruby-on-rails

I have some object files which I want to have them in my JS codes I put them under app/assets/3d-models and when I try to get a link to it I use:
<%= asset_path("3d-models/splits2/xxx.obj") %>
But the output is /3d-models/splits2/xxx.obj which obviously the 404 NOT FOUND is the result(i.e the asset not found, the wrong link!)
Question:
How can I get an access link to a file other than common files used in rails' assets?

You can have different assets. Just place them into assets folder and link to them correctly.
Rails are using assets pipeline. This is a concept, which provides a better way to serve some static files to a website. In Rails it is implemented by Sprockets gem. Please read more here.
You can also have other static files directly in public folder and link to those files anywhere from your app. This will not be included in assets pipeline.
If you use assets_path helper method, then there is a fingerprint added to a file so the URL to the file is different then.
Please check this section.

Related

Rails 4 Heroku Assets not loading, though in heroku asset pipeline

I have a problem with certain assets on heroku. (local environment is working fine)
The assets are in the pipeline. If I execute in the heroku rails console:
helper.asset_path("typicons.woff")
helper.asset_path("backgrounds/1.jpg")
I get the following response:
/assets/typicons-c2430aad2b6a33948dc064cfaee8ad65ff9e3ca439834f3aaa84abec3d10dea8.woff
/assets/backgrounds/1-c2098ff7e7fbb89b2d18e9cd9089f712f2b837265d1d2e4182c36c23392760c6.jpg
So I assume that the assets are in the heroku asset pipeline. As well by opening the url directly with the digest in it, I receive the file.
However if I try to reference the files in css or javascript like this:
$('.top-content').backstretch("/assets/backgrounds/1.jpg");
The file does not load. As well opening /assets/backgrounds/1.jpg directly does not work. Referencing assets from .rb or .erb files works.
Please can someone tell me, what kind of config I have to change, so the URLs for assets work as well without the digest?
Thank you!
Assuming you are using a fairly standard asset pipeline setup, this passage from the Rails Guides should help:
If you add an erb extension to a JavaScript asset, making it something such as application.js.erb, you can then use the asset_path helper in your JavaScript code:
-- http://guides.rubyonrails.org/asset_pipeline.html (section 2.3.3)
In your example, add the erb extension to your JS file and then change the line to
$('.top-content').backstretch(<%= asset_path("backgrounds/1.jpg") %>);
The problem is that Rails 4 does not support non-digested assets at all.(For whatever reason)
Here is a more thorough explanation on the issue: Non Digested Asset Names in Rails 4
My personal workaround was to add a public asset folder to my app:
public/assets/static/
and upload the assets required there. Since it was only about fonts and background images, which do not change often, it does not seem to be a problem. In the link above there are several other solutions proposed.

Am I handling the 'public' directory the right way?

I am using Ruby on Rails 4.1.1 and I want to add a linked image - my app's logo - in email messages but I had a doubt since my previous question.
By using files located in the public directory it seems that to link to an image in your Asset Pipeline you can use
link_to(LINK_TEXT_OR_IMAGE_TAG_HELPER, image_path(IMAGE_NAME))
but for those located in the app/assets it seems do not work the same, at least in rendered email messages in production mode. All assets that are compiled in Production have a fingerprint ID... is the fingerprint causing the load of static assets to do not render images in email messages?
I doubt since I would like to access images from both browser and email. I tried to solve the issue by moving the image from app/assets/images/logo.png to public/images/logo.png and by changing statements in my application.css.scss file from image-url("logo.png") to url("/images/logos.png"). However I do not know if I am following "the Rails way" or a "best practice". Do I? Should I add to the public directory all assets that I plan to use outside my application and in the app/assets directory all assets that I plan to use internally to my application?
For emails, it almostisn't any different compared to standard Rails views.
You can link to an image in your mailer using the image_tag helper you'd normally use in regular views as well:
<%= image_tag('some_image.jpg') %>
You also need to tell Action Mailer where it can find the assets, because it will use absolute URLs to refer to your images:
config.action_mailer.asset_host = 'http://www.example.com/'

How can I drive Rails asset pipeline from a controller?

In Rails, I want to drive the asset pipeline to compile and minify my js file and get the result as a string. The js file uses sprockets to combine multiple files together.
How can I do this?
This needs to be run from a controller action. It's not as simple as just serving an asset, rather, I want to get my compiled and minified JS as a string and then do something with it.
You can do something like this to access your asset.
view_context.asset_path 'your_file.js'
Check this question also
How does one reference compiled assets from the controller in Rails 3.1?

When using asset pipeline, how to stylesheet_link_tag for static CSS assets?

I'm trying to use the asset-pipeline with sass-rails. The problem I'm having is I have some CSS from libraries that I don't want to be compiled with the rest of the stuff.
I tried putting it in public/stylesheets, but when I try to include the file in my app I get Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError
What do I need to do to get Rails to look at the right place for static CSS files? Or is there some default place where I'm not putting it properly?
Edit: Double-checked and it doesn't work for JavaScripts either. Reading http://bibwild.wordpress.com/2011/12/08/jquery-ui-css-and-images-and-rails-asset-pipeline/ seems to suggest that Rails simply doesn't check the public dir by default, and we have to manually specify the path?

Precompiled assets and Non-compiled assets

Can I use assets that aren't pre-compiled along with assets that are inside a rails application?
I have separate CSS files uploaded through a web interface and don't want to precompile my assets everytime I add a new CSS file because a) that would be way too manual of a process and b) the styles all go to one page, like a theme, and so use the same class names. These CSS files do not require any processing as they are simple CSS files.
The CSS files are in a sub-folder of the assets folder inside the public folder: public/assets/styles/.
Is this possible?
Yes it is possible.
If i'm not wrong this SO link is very close to what you were trying to achieve
Rails assets:precompile only one asset?
Hope it helped !

Resources