How do I add csrf_meta_tags to my .coffee.erb file? - ruby-on-rails-3

I have a .coffeescript.erb file in which I would like to get CSRF meta-tag information in Rails 3.1. The file is called bookmarklet.coffee.erb and it is in my assets/javascript folder. This is the code I am using:
csrfMeta = <%= csrf_meta_tags %>
When I access assets/bookmarklet.js, I see this in my server log
Error compiling asset bookmarklet.js:
NameError: undefined local variable or method `csrf_meta_tags' for #<#<Class:0x007f83d2efc3a8>:0x007f83d4043080>
How can I get the CSRF meta tags into my javascript file?

I managed to get the CSRF tags to show up by moving the file into my views/parser folder, and renaming it as bookmarklet.js.coffee. Instead of using csrf_meta_tags, I used the helper form_tag which automatically includes the CSRF tags.

Related

Hanami routes helper don't work in templates

I have hanami application version 1.0.0
I have next routes.rb file:
get '/games', to: 'games#index'
root to: 'home#index'
I read docs and tried to use
<%= routes.games_path %>
in application.html.erb template, but recieved next error:
Hanami::Routing::InvalidRouteException: No route (path) could be generated for :games - please check given arguments
How can I use routers in hanami templates?
Instead of using resources, you can also add a name to your route:
get '/games', to: 'games#index', as: :games
Read more at: https://guides.hanamirb.org/routing/overview/
I added
resources :games, only: [:index]
to my routes.rb file and this fixed my problem.

gmap4rails - can't find file underscore / Gmaps is not defined

I am following the simple tutorial here: https://github.com/JonKernPA/gmaps
I keep getting the error:
couldn't find file 'underscore'
(/app/assets/javascripts/application.js:16)
with the following line highlighted:
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
I tried following the suggestions posted in
Gmaps is not defined in rails 4 + gmaps4rails 2.0.3
and I get the same error with the error with the same line highlighted.
When I try to remove the line
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
from application.html.erb, I get the error "Gmaps not found"
Any help appreciated, thanks.
Github link: https://github.com/chiefkikio/circus/
Found a solution. Had to install gem 'underscore-rails' and then bundle it and restart the rails server.
Not a tutorial project, but I use gem 'underscore-rails' (cf https://rubygems.org/gems/underscore-rails) to satisfy gmaps4rails' dependence.
The minified version of Underscore has this line at the end of the file:
//# sourceMappingURL=underscore-min.map
Chrome's developers tools will try to download underscore-min.map when encountering this line. Users won't see this error, unless they open developer tools.
If you want to get rid of this error, either add underscore-min.map to your project (https://github.com/jashkenas/underscore/blob/master/underscore-min.map) or remove that line from underscore.min.js
I also faced the issue and this is what was the reason for me:
The order of required files in my application.js was wrong. I required
//= require_tree . before gmaps. And since I didn't wait until page load in my controller_name.js file, it was loaded first, trying to use Gmaps before the latter was defined.. I thought it might help, but.. I run into another couple issues with ".. is not defined". After small research here is the solution:
wrap you whole custom js in callback for window.load event. I used it with this line:
google.maps.event.addDomListener(window, "load", callback)
Hope this will help

Rendering CSS using PDFKit

I know that there have been other questions about this, but I've read through them all and haven't figured this out yet. I'm using the PDFKit gem to allow users to generate .pdfs of content on my site. I can render a pdf by adding .pdf to the url, but CSS doesn't transfer along with it. I understand from Googling that it's because of the asset pipeline, but I haven't figured out how to make it work.
I've installed both of teh necessary gems:
gem 'pdfkit', '~> 0.6.2'
gem 'wkhtmltopdf-binary'
In my application.rb file I added 'require pdfkit' at the top and the following to the application class:
config.middleware.use PDFKit::Middleware
config.cache_classes = true
config.eager_load = true
I then ran rake middleware
I also added an initializer called pdfkit.rb with teh following code that I found, in order to fix the .pdf from endlessly loading:
ActionController::Base.asset_host = Proc.new { |source, request|
if request.env["REQUEST_PATH"].include? ".pdf"
"file://#{Rails.root.join('public')}"
else
"#{request.protocol}#{request.host_with_port}"
end
}
At this point adding .pdf to a url creates the pdf, but doesn't render the CSS at all. The README on pdfkit's github says:
"Resources aren't included in the PDF: Images, CSS, or JavaScript does not seem to be downloading correctly in the PDF. This is due to the fact that wkhtmltopdf does not know where to find those files. Make sure you are using absolute paths (start with forward slash) to your resources."
My CSS is called in the head as usual:
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
How do I make an absolute path? I don't think I just replace the stylesheet_tag with an ordinary
<link rel='stylesheet' href='bootstrap_and_customization.css.scss' type="text/css">

Rails 3 asset pipeline: How to prevent some asset files from being loaded for everyone (eg. browser-specific)

I want to server the 'excanvas.min.js' file only to the Internet Explorer and not to other browsers.
So I did in my application.html.erb:
<!--[if lt IE 9]>
<%= javascript_include_tag 'excanvas.min'
# Excanvas for use of Canvas in IE up to version 8
%>
<![endif]-->
<%= javascript_include_tag 'application' %>
But I get the following error (in production):
Completed 500 Internal Server Error in 3ms
ActionView::Template::Error (excanvas.min.js isn't precompiled)
The error also happens when forcing Rails to precompile the file with
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
config.assets.precompile += %W(excanvas.min.js,other_file.css,even_anotherfile.js)
in 'config/production.rb'. What am I doing wrong?
I could do a
//= require excanvas.min
in the application.js. But that would include the file for every browser...
Info: I made an update from an Rails 3.0-Application.
The problem is in the following line
config.assets.precompile += %W(excanvas.min.js,other_file.css,even_anotherfile.js)
the %W is a shortcut for creating an array, and the items need to be separated by a space. In your case you are adding the string excanvas.min.js,other_file.css,even_anotherfile.js to the precompile list.
So just replace the line as follows:
config.assets.precompile += %W(excanvas.min.js other_file.css even_anotherfile.js)
just try to add <%# %> for line and .
i think they will be displayed as comment.
OR
you can do that will js.judge the browser with js and load the corresponding css file

How to get debug mode for CSS?

<%= stylesheet_link_tag 'application', :debug => Rails.env.development? %>
Doesn't seem like its working. I think the asset is still being precompiled... I can verify this because firebug ain't reporting the right lines.
I suspect that you may not have this option set in your development.rb file:
config.assets.debug = true
With this on Rails will render a link (or script) tag for each file. You don't need a :debug option in your layout file unless you want to turn debug mode off (=> false).
See the Rails guide on using the pipeline in development mode for more information.