Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Javascript Rails Admin

I want to add the JS from Google Analytics embed API into my rails application and only for Rails Admin (not needed anywhere else).

We have overwritten the Rails Admin Dashboard and now we are struggling with the custom JS.

I found here: https://groups.google.com/forum/#!topic/rails_admin/KgUl3gF5kTg that it needs to be placed in app/assets/javascripts/rails_admin/custom/ui.js.

So I've placed all the .js files and the .js.map files in the directory:

.
└── custom
    ├── active-users.js
    ├── chart.js
    ├── datepicker.js
    ├── moment.js
    ├── platform.js
    ├── platform.js.map
    ├── polymer.js
    ├── polymer.js.map
    ├── promise.js
    ├── ui.js
    └── viewpicker.js

And I've added //= require_tree . in the ui.js file but in rails_admin I still receive:

Uncaught ReferenceError: Polymer is not defined 

This means that the .JS file isn't loaded.

Thanks to this link for finding the above link: Rails Admin: add javascript library to custom action

Edit 1: Using Rails Admin in a Rails 3 env.

Edit 2:

So for testing reasons I removed all the custom JS/HTML etc.

I've set this in the ui.js:

//=require_tree .

I've set this in leecher.js:

$(document).ready(function() { document.MY_SIMPLE_VARIABLE = 2; } );

When I restart the server I go to rails admin, log out, restart the server again, log in. Go to console in chrome and type:

document.MY_SIMPLE_VARIABLE

And I receive the following: enter image description here

For testing reasons I've added an:

alert("hello world");

But still no trigger.

The location of the ui.js and leecher.js is:

app/assets/javascripts/rails_admin/custom/

And this my current Gemfile:

http://pastie.org/private/zrrzjpbx5jq7enpeknsa

Edit 3: If I put the JS code into the ui.js it shows an alert! So they are problems with the includes (//= require tree .)

Edit 4: All the JS files I'm using: https://gist.github.com/YOUConsulting/243397c31ea28d217367

Simple example that doesn't work: https://gist.github.com/YOUConsulting/571e785d6b6c1ed06d6b

Simple example that does work: https://gist.github.com/YOUConsulting/52de78aa043239aae707

like image 254
FastSolutions Avatar asked Aug 21 '14 08:08

FastSolutions


2 Answers

I've rewritten my answer to describe the complete solution in case other people need this in the future.

app/assets/javascripts/rails_admin/custom/ui.js works like a manifest file in the Rails asset pipeline, thus it supports these directives.

In order to include all .js files under custom, add

//= require_tree .

at the top of the file. If that does not work right out of the box, running

rake tmp:clear

will solve the problem ;)

Cheers!

like image 200
Cec Avatar answered Oct 30 '22 07:10

Cec


After talking more to Cec on this topic. He found that a simple rake tmp:clear, was the correct answer. See answer above or below.

like image 23
FastSolutions Avatar answered Oct 30 '22 09:10

FastSolutions