Access stylesheet_link_tag from controller - ruby-on-rails

Is there a was to call stylesheet_link_tag from the controller? I am creating a PDF file in memory and passing it along to an api call to another service. I am using PDFKit and it requires me to send the style sheet link to it. I am using Rails 3.1 and therefore need access to the asset pipeline through this method.
Thanks for the help!

You should be able to use this to access the stylesheet from your controller:
ActionController::Base.helpers.asset_path("stylesheet_i_want.css")

This question is closely related to this one:
How does one reference compiled assets from the controller in Rails 3.1?
See my answer there, but, more briefly, you can access the Rails asset pipeline, which is managed by the Sockets library, from Rails.application.assets. That will be a Sprockets::Environment instance, documented at the Sprockets project. You can use it like this:
Rails.application.assets['application.css'].pathname #=> "/home/username/project..."
Rails.application.assets['application.css'].to_s #=> "html, body { ..."

Related

Accessing Rails images by direct path

I am integrating a Angular app with Rails and I have a problem with accessing the images. Angular app has a lots of path to images with structure: (for example)
src="assets/img/layout/ico-przystawki.png"
But that doesn't work in a Rails application, because I shouldn't be using the 'img' part. It should be like this:
src="assets/layout/ico-przystawki.png"
Is there a way to tell Rails application to use direct paths to images, so that I don't have to change every time these paths?
I found myself using the non-stupid-digest-assets gem for this very purpose. See https://github.com/alexspeller/non-stupid-digest-assets
Stop assets precompile and then you can access all img (and assets file) with src="assets/ut/ico-przystawki.png"
Try to use the provided rails helper method image_tag instead when you call an image:
<div>
<%= image_tag("ico-przystawki.png") %>
</div>
Read more about that here:
http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag

Rails I18n doesn't work with asset pipeline templates in Rails 3.2.11

I need to localize an app, so I created a file for Spanish translations, but the config/locales/es.yml file doesn't seem to be properly loading, or maybe it loads after my templates are evaluated.
First a little context, I'm using the handlebars_assets and haml_assets gems. The latter allows to use the ActionView helpers within the templates in the asset pipeline and it also has access to the application classes, so one could write templates like:
.some_div
= SomeModel.model_name.human
And it works, except that it's returning the fallback name of the model. If I make these kinds of translations in a view though (instead of .hbs.haml template) I get the proper translated string. This is also true if I execute the translation methods in the rails console.
Following the Rails Guides I created the following initializer:
I18n.default_local = :es
Which is working properly if I consider that translating in views renders the expected output. My application is mostly living on the client side, so I really don't use Rails' views, it's a single page application managed with the help of Backbone. All the UI lives in templates that get rendered through the asset pipeline. So my question is, has anyone managed to make I18n work with this setup?

how to use ajax in rails3.1?

As we know, Rails 3.1 is using CoffeeScript and JQuery.
The old way that I handle the ajax request is respond the request to a template which name is some_action.js.erb
Now that we have CoffeeScript and I want my template could use coffeescript syntax.So what can I do?
Just changing the template file's suffix name?
some_file.js.coffee this works fine in the assets/javascript directory.
But I am handle the ajax request and its template is under the folder views,if just simply change the template name from some_action.js.erb to some_action.js.coffee.erb or some_action.js.coffee,it won't work,rails treat it like normal file and will not compile it from coffeescript syntax to normal javascript.
I am very sorry about my Enlgish ability,hope this time my question will explain my purpose.
Assuming you have upgraded to Rails 3.1, taking advantage of the new syntax is, like you suggested, as easy as converting your files over to CoffeeScript.
Now, you can't just name the file *.js.coffee and expect a conversion, but Ryan Bates at Railscasts recommends changing your files over manually as a great way of learning the syntax yourself. In fact, he has a screencast posted of him doing just that: http://railscasts.com/episodes/267-coffeescript-basics
Rails, then, will handle the compilation from js.coffee into .js for you.

Rails 2 route helpers in plugin

I'm trying to write a plugin, and among the tasks I want to perform I want to be able to call route helper methods from within the plugin. For instance, if I have map.resources :user, I want to be able to call user_path(:id => 1) from my plugin. I keep getting undefined method user_path error.
In rails 3, you can do this using Rails.application.routes.url_helpers, but I don't seem to be able to find an alternative for rails 2. Including ActionController::UrlWriter does not help. Any ideas?
I'm using rails 2.3.4 and i can use my regular path helpers in the controllers and views of my plugins, at least within the ones i tested.
I can't use them in the lib files for the plugins, but that's because the helpers aren't available outside the controllers (the views are dealt with inside the controllers so they can use them too). The lib files (the meat of the plugins) tends to be modules and classes which get loaded into the model environment.
Can you provide more details about what you're trying to do?
You should be able to do:
app.user_path(1)

Rails 3.0 beta & JS Helpers with jQuery

In the release notes for Rails 3.0 beta it says:
"Unobtrusive JavaScript helpers with drivers for Prototype, jQuery"
So how do I setup Rails 3 to use jQuery then? It still loads all the Prototype libraries by default.
I took this to mean that Rails 3 has built in functionality similar to the jRails plugin, but maybe I'm misunderstanding :)
Also, as a bonus question, if I am using Prototype is there a way to get Rails to load the minified versions, and even better a single concatenated JS file to cut down on http requests?
Thanks.
When you create a Rails 3 app, just pass along the -J param as well:
$ rails app_name -J
This will skip over including the Prototype libraries. Now, all you need to do is drop the latest jquery.js file into the public/javascripts directory. Once you do that, you'll also need the jQuery version of the rails.js file. You can get that here:
http://github.com/rails/jquery-ujs/blob/master/src/rails.js
EDIT: You need to include these files in the top of your layouts to gain the functionality. You can do this by:
<%= javascript_include_tag "jquery", "rails" %>
Hope this helps!
Unobtrusive JS doesn't refer to the PrototypeHelper methods, but to remote forms and links and the like. The concept is that you include :remote => true in your form_for or whatever helper methods support it, and then a driver called rails.js will look for those remotes and intercept the submit or click events and send them via xhr.
To use jquery you'll just need to replace the prototype ujs driver (which ships with rails) with the jquery ujs driver, that was extracted into its own repo shortly before the rails 3 beta release. You can find it here.
Check out Google Closure
It can turn multiple javascript files into a single compressed js file. It can even figure out which parts of the library you aren't using and remove them as well.
I don't know about Rails 3, but I'll try to answer bonus question.
You can put whatever you want in public/javascript directory. By default it will load files: prototype.js, effects.js, dragdrop.js and controls.js (read more). If you want to compress all js files and send it in one file, you can use this plugin.
Of course it won't work for dynamicaly generated js files.
I'm also working on that with trying to convert my old ajax with rails 3.
From what I can tell, they moved to a structure that adds a data-remote=true and when you add :remote => true to something like link_to which is supposed to replace link_to_remote in rails 3, so there are no more onclick methods that calls Ajax methods.
So, how does Ajax work in Rails 3, then? Well, you are supposed to have javascript methods that watch for when you click links that have a property of data-remote=true and does something with it, but don't actually include them in Rails (from what I can tell), it's library agnostic since you can write these methods to watch for clicks in prototype, jquery, write them yourself, or whatever else is out there.
I did find some javascript on github to get started that will watch for these events:
In prototype
In jQuery
I think in order to actually load jquery instead of prototype, you're going to have to just download it to public/javascripts and manually specify jquery, use javascript_include_tag :all, or override javascript_include_tag (not recommended)

Resources