I'm using RoR and jquery.ui.addresspicker.js
This jquery.ui.addresspicker.js requires that google is loaded before that library is loaded. I only need the addresspicker on a couple of pages in the application.
The google part that needs to be loaded before is this:
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
But this makes the page load slower so I don't want this on all my pages.
The application.js contains this:
//= require jquery
//= require jquery_ujs
//= require jquery.ui.all
//= require jquery.blockUI
//= require select2
//= require cocoon
//= require_tree .
Currently the jquery.ui.addresspicker.js file is in app/assets/javascripts and thus also loaded automatically all the time.
How can I handle this? Should I move the addresspicker.js out of the javascripts directory? Should I rewrite my application.js? Other suggestions?
You can replace
//= require_tree .
with an explicit list of the javascripts that you want included on each page. and just include jquery.ui.addresspicker on the pages that you need it.
I used Ian's idea to change the application.js
I created a new folder named sitewide and copied all the javascripts in there that I want to use on the whole site (so not the addresspicker).
I changed application.js like this:
//= require_tree ./sitewide
And in the page where I needed the addresspicker I put it using javascript_include_tag
This way I don't need to change the application.js file everytime I want to add a js file.
use a different layout on both pages or just remove the jquery.ui.addresspicker.js from the default_layout and just include in the pages where you need it
<%= include_javascripts "jquery.ui.addresspicker" %> or
javascript_include_tag "jquery.ui.addresspicker"
in the page where you need
Related
I have to modify a RoR project. All the javascripts are in the public/assets folder and my work is to move them in the app/assets/javascripts folder.
But when I try to import my javascript in my view with that : <%= javascript_include_tag "application"%> the result in code is the folowing :
<script src="/javascripts/application.js">
{"status":"ERROR","message":"404 not found"}
</script>
Currently here is what I've got in my folder
I haven't modify the application.js. The file contain these lines :
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
I also modify my config/application.rb to add this line :
require 'sprockets/railtie'
I don't know why I have a 404 not found. In all the tutorials that I read it looks like that all I have to do is to put my JS in app/assets/javascripts and use javascript_include_tag to access it. But I suppose that some configurations lines are missing but I can't figure out which.
Thanks for the help
I have a kind of strange problem with assets.
In application.html.erb trying to include controller specific assets this way
<%= javascript_include_tag "application", params[:controller] %>
application.js looks like this:
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require leaflet
//= require select2
//= require_tree ./general
//= require turbolinks
General folder contains some common JS files. Application uses 2 controllers: Index and Profiles (root route is "index#index"). At some moment Rails starts to load assets in wrong way: on index page profiles.js are loaded and index.js on /profiles/new. This problem only appears when I press index or create profile links in navbar. No problems seems to be opening this pages in separated tabs or just reloading page using Ctrl+R. So I assume that it's some turbolinks problem?
I have a Rails application, and I'm using Ember on the front-end. I'd like to move the ember-related files down one level in the directory structure, but when I do, the templates no longer render.
In the plain, vanilla, working version of the application, my directory structure is:
./app/
assets/
javascripts
application.js
ember-app.js
routes.js
store.js
models/
controllers/
routes/
templates/
views/
with: application.js
//= require jquery
//= require jquery_ujs
//= require handlebars
//= require ember
//= require ember-data
//= require_self
//= require ember-app
App = Ember.Application.create();
and: ember-app.js
//= require ./store
//= require_tree ./models
//= require_tree ./controllers
//= require_tree ./views
//= require_tree ./helpers
//= require_tree ./templates
//= require ./router
//= require_tree ./routes
Everything works fine. However, I would like to move the ember-app file and all ember javascript code down one level, and when I do so, the templates do not render. (Part of the application uses Ember, but not the entire application, and I'm trying to set up two separate paths through the asset pipeline.)
The desired structure is:
./app/
assets/
javascripts
application.js
embro/
ember-app.js
routes.js
store.js
models/
controllers/
routes/
templates/
views/
with: application.js (revised: 'require ember-app' becomes 'require embro/ember-app')
//= require jquery
//= require jquery_ujs
//= require handlebars
//= require ember
//= require ember-data
//= require_self
//= require embro/ember-app
App = Ember.Application.create();
(ember-app.js is unrevised.)
As I said, after the move, none of the template content appears onscreen. No errors onscreen or in the console, just an empty ember-application.
When I examine Ember.TEMPLATES in the console, all the expected templates are listed. Furthermore, if I put the desired content in x-handlebars templates in the appropriate rails view, the content successfully renders, just as it did with the original directory structure.
For example, in apps/views/welcome/index.html....
<script type="text/x-handlebars" data-template-name="application">
<h1>hello</h1>
{{ outlet }}
</script>
<script type="text/x-handlebars" data-template-name="index">
<h1>this is the index</h1>
</script>
... and we're good to go again.
but if I leave the rails view empty, as I did with the original structure, it's a no go.
Wondering if perhaps the ember-rails gem requires the handlebars templates to be present in app/assets/javascripts/templates, and if there's a way to override this. The documentation mentions adding a templates_root option to the application configuration block, and I'm wondering if this is the key. I've played around a bit, no luck yet.
Any ideas?
UPDATE:
Afraid I'm not having any luck with the templates_root option. As an experiment, I tried building a new, simple rails app, and using the ember-rails bootstrap generator to get it up and running. All's well, but if I then attempt to simply change the name of the templates folder (i.e. app/assets/javascripts/templates -> app/assets/javascripts/temple), with appropriate changes to the sprockets includes and config files, I'm getting the same results.
Any chance the templates_root option is somehow broken?
I'm using Ruby 1.9.3, Rails 3.2.11, ember-rails 0.10.0
Any pointers to where I should look in the ember / ember-rails / handlebars source code? Have started poking around.
thanks!
You're right that you need to set templates_root. Try adding
config.handlebars.templates_root = 'embro/templates'
to the configuration block in application.rb, or
RailsApp::Application.config.handlebars.templates_root = 'embro/templates/'
to a new initializer, where RailsApp is whatever your application is named.
Edit:
I was able to reproduce the behaviour that you described with templates_root. The fix for me was to delete the /tmp folder of my application and restart rails. After that, the templates were named correctly.
Edit:
More precisely, you need to clear the sprockets cache at /tmp/cache/assets after changing templates_root.
Edit:
As mentioned in the comments below, a simple rake tmp:cache:clear should take care of the problem.
This is driving me absolutely bonkers, and it seems like such a simple thing to implement. I can't get tooltips (or, for that matter, popovers) to work. I have the 'twitter-bootstrap-rails' gem installed, and I generated the appropriate .js and .css files.
I don't think it's a JQuery issue because I have other JQuery functionality on my site that works perfectly.
Here's some relevant code.
application.js
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require bootstrap-tooltip
//= require bootstrap-popover
//= require prototype
//= require prototype_ujs
//= require effects
//= require dragdrop
//= require controls
//= require_tree .
$('a').tooltip();
application.css
*= require_self
*= require_tree .
*= reqire_twitter/bootstrap
Code I'd like to get to work for tooltips
test
The above code, when hovered over, just shows Chrome's normal tooltip. I'm getting the same behavior in Safari, so I doubt it's browser-specific.
I'm using Rails 3.2.7, adding Bootstrap manually. Everything works just fine.
application.css
*= require style
*= require bootstrap.min
*= require bootstrap-responsive.min
*= require_tree .
application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap.min
$(document).ready(function(){
$('a').tooltip();
});
//= require_tree .
and layout/application.html.erb
<head>
<%= stylesheet_link_tag("application", :media => "all") %>
</head>
<body>
<%= yield %>
<!-- scripts concatenated and minified via build script -->
<%= javascript_include_tag "application" %>
<!-- end scripts -->
</body>
</html>
Your application.js code will be executed before your web page is fully loaded into the browser, so your line of code will not find any element prior to the page load.
You will need to execute when the DOM is fully loaded:
$(document).ready(function () {
$('a').tooltip();
});
The solution to this was two-fold.
Change the $ in the code to jQuery. This got rid of the "Object # has no method 'ready'" error.
I had a bootstrap.js.coffee file that had irrelevant code. When I deleted the file, everything began working again. This got rid of the "Uncaught TypeError: Cannot call method 'tooltip' of null" error.
After much fighting, in order to get it working properly with Rails 3.2 I had to do the following:
in application.js:
//= require bootstrap
in the view template where I want the tooltip, include the rel='tooltip' attribute:
= link_to "Link text", title: 'Click to reveal tracks', 'data-placement' => 'top', rel: :tooltip
or if you're not using HAML:
Link text
in any of the coffeescript files (I created a new one) put this:
jQuery ->
$('[rel="tooltip"]').tooltip()
This works, though the styling for the tooltips are not working, I can't figure out why, but the javascript seems to be working at least.
Hopefully this will help someome else.
As someone commented already, bootstrap-sass includes a coffeescript file which has this in it:
jQuery ->
$("a[rel=popover]").popover()
$(".tooltip").tooltip()
$("a[rel=tooltip]").tooltip()
If you call this file there is no need to include the other coffeescript file above since this one does the same thing. In my case I already have bootstrap.js called from application.js so this one wasn't called (I removed require_tree so I can call the files I want in the order I want).
For site wide specific JS code (i.e. for the header, which appears on all pages). Where should this be placed? In:
app/assets/javascripts/application.js
Is that right?
For pages#home. Which root_url also points to (root :to => 'pages#home'). Where should my JS file be placed in the pipeline?
app/assets/javascripts/pages/home.js
And regards to my application.js. Is this right? It currently looks like:
//= require jquery
//= require jquery_ujs
//= require_directory .
Or should I just embed page specific JS in the view?
It's not a good idea to put js code in application.js. You can put that code in any other file on the assets/javascripts folder and it will be included automatically by the require_directory or require_tree command. Your application.js file is perfectly fine as it is right now, but you might want to use require_tree instead of require_directory for recursive inclusion.
For example, the javascripts files in app/assets/javascripts/pages will be included by require_tree but not by require_directory.
Recommended reading: Asset pipeline guide