Heroku and Rails 4: some javascript not working - javascript

I'm trying to get a javascript item displaying on Heroku Rails 4 applicarion. On my local development machine, everything is running perfectly. Can't get it to work on Heroku.
In my application.html.erb file I have added:
<%= javascript_include_tag "application" %>
and in another view file (events.html.erb) I have:
<script type="text/javascript">
$(document).ready(function()
...code to add data to a timeline using getjson()...
</script>`
which is referring to a timeline.js file in the assets>javascripts folder. When an event is added (using Rails CRUD operation, the javascript code is calling a getJson() method and is adding the event to a timeline. This is working as expected on local machine.
When I push to Heroku, I can add events through rails, but the timeline is not displayed.
My initial idea was that Javascript was not being loaded on Heroku, so therefore I added a simple:
<script type="text/javascript">
alert('Displays OK')
</script>
call and this displays just fine on Heroku (as well as on my local machine).
When looking at the page source on Heroku, I can see:
<script src="/assets/application-6da332d35efc3706e98f87ead9900f20.js">
so it appears that the timeline.js file is precompiled (it seems a minimized version of the timeline js).
On my local machine, I don't see the precompiled js file, but I see:
<script src="/assets/timeline.js?body=1"></script>
<script src="/assets/timeline.min.js?body=1"></script>
The fact that on Heroku, it displays the javascript alert, but not the timeline, seems to indicate that something is messed up with the javascript code in the precompilation.
I can't see anything obvious in the heroku logs. It says slug compilation was finished successfully and I can't see error messages.
Is there a way I can enforce Heroku to not use the precompiled js file, so I can have on Heroku:
<script src="/assets/timeline.js?body=1"></script>
<script src="/assets/timeline.min.js?body=1"></script>
I have the following in production.rb file:
config.serve_static_assets = false
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.compress = true
config.assets.digest = true
config.assets.debug = false
Hope someone can help me on this. This has been bugging me for couple of days now. Any help appreciated.
Thanks!

I think you need to specify that timeline.js must be precompiled.
On config/environments/production.rb, add this :
config.assets.precompile += %w( timeline.js )
In you view, include you timeline js with :
<%= javascript_include_tag "timeline" %>

Related

Load json file into Asset pipeline

So, I'm trying to use Cocos2d-JS to create a simple game embedded into one of my rails view.
The thing is that cocos requires a farly simple project.json file with some config data in it to run correctly, here's the internal code:
if(!txt){
txt = cc.loader._loadTxtSync("project.json");
}
data = JSON.parse(txt);
Nonetheless even though I have this JSON file besides the cocos2d-JS .js file, I get always the same error running the rails server on development which is:
GET http://127.0.0.1:3000/project.json 404 (Not Found)
What have I tried so far and haven't worked:
Change the path on the js file to use a relative one, doesn't work.
Putting the JSON file in an aws bucket and try loading it from there, I get a non-authorized access error.
Renaming the cocos2d.js file to cocos2d.js.erb and lad the json with <%= asset_path 'project.json' %>, this breaks cocos2d itself...
How to bypass in case I can't get an answer:
I would try fixing the aws non authorized access issue, I'm a bit seasoned in Ruby and Rails, but don't fully understand the asset pipeline in order to find an alternative...
So my question is: Is this something related to the asset pipeline? How can I load this .json file into my js?
Thanks a lot!
Fixed!
Just modified the CORS policy on aws to allow access from my web app and everything's done

How do I include custom javascript on/for a single page in Rails?

I have an interactive header for my site located in /views/layouts/_header.html.haml.
I want all the JavaScript for the header to be collected into a single file. Most importantly, I'm using Twitter Typeahead and Bloodhound for a search field with auto-suggest. A lot of this JavaScript has to be run after the header is rendered, so its inclusion in application.js (which is included on my page in application.html.haml) doesn't work, as this runs before the header is rendered.
I added search-bar.js under /assets/javascripts/ which contains all the JS I need to run on this page.
At the bottom of my _header.html.haml I just linked with a regular script tag.
%script{src:"/assets/movie-search-bar.js", type:'text/javascript'}
This works fine locally, but on my dev server I get a 404 for that asset. Is it possibly throwing out the static file for performance reasons? Even if it does work, by having it included in the asset pipeline, wouldn't the script be loaded twice (once as an individual script, and once in application.js)?
I'm getting the impression that this is not the best way to isolate the javascript of a partial into its own file. What is the best and most "railsy" way to ensure that the script is loaded once, after _header is rendered, and isolated within its own JavaScript file?
EDIT: Could this perhaps be as simple as moving the following lines to the bottom of my footer partial? Is this considered good practice in Rails?
#{ stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true }
#{ javascript_include_tag 'application', 'data-turbolinks-track' => true }
You can use javascript_include_tag on scripts other than application.js. First, make sure your script is in app/assets/javascripts folder: app/assets/javascripts/movie-search-bar.js.
Then use the javascript_include_tag helper in your view:
javascript_include_tag 'movie-search-bar'
And tell Rails to precompile that asset.
config.assets.precompile << "movie-search-bar.js"
Well I have done this task .If you want to include a particular javascript in your view .First create a folder in your views folder .For example page_js and then create some partial like _file.html.erb .And place your javascript into it .And then render that file wherever you want .So your javascript will work for that particular page only .If it doesn't work then tell me .

Liferay portlet deploy timestamp

I need to include some JavaScript in jsp pages:
<script type="text/javascript" src="<%=request.getContextPath()%>/newsletter/js/newsletter.js"></script>
When I deploy a new portlet version and I change this JavaScript file, some users see the old version of this file.
I saw that Liferay adds last deploy timestamp to javascripts (...js?t=14573725543), and I can do it if I add this file to liferay-portlet.xml. But I can't do it with newsletter.js. Sometimes I need no load this file.
Is it possible to add manually this automatic timestamp? I do something similar in a theme with $theme_timestamp.
Liferay is using com.liferay.portal.model.Portlet.getTimestamp() to generate the number after t=. You can get the (Liferay) portlet from the PortletContext:
<%
LiferayPortletContext context = (LiferayPortletContext) portletConfig.getPortletContext();
long timestamp = context.getPortlet().getTimestamp();
%>
<script type="text/javascript"
src="<%=request.getContextPath()%>/newsletter/js/newsletter.js?t=<%=timestamp%>">
</script>

Accessing javascripts in rails

I am completely new to Ruby on Rails and I'm trying to serve an application in which an html file calls javascript that is located in a separate file. I placed the javascript file in the javascripts fold of my rails application. My html displays fine but when I press a button that is intended to call a javascript function I receive an error.
Uncaught TypeError: Cannot call method 'myMethod' of undefined
I can see that the file is present by looking in the resources tab of google chromes developer tools. It also looks like rails is correctly including the javascript file in the html file.
<script data-turbolinks-track="true" src="/assets/myScript.js?body=1"></script>
I'm not sure what I'm doing wrong. Any help would be greatly appreciated.
You should really use the javascript_include_tag helper to keep the app to convention:
<%= javascript_include_tag "myScript" %>
The error is to do with your Javascript's call to myMethod. The error will be caused by referencing myMethod on an object / var which doesn't exist

Rails and JavaScript: Can't add .erb extension

I have a JavaScript file called users.js and it is in app/assets/javascripts/users.js. I have it load in a single page using
<%= javascript_include_tag params[:controller] %>
I was thinking of using some Ruby code in the JavaScript file, so I changed the filename to users.js.erb. But when I do, the file shows up in my SublimeText project view as users.js.erb.js, and when I run my Rails app, I get a 404 error saying users.js.js couldn't be found.
Help? Thx.
From my perspective, this is not a rails problem, but a problem with the editor you use. Check the file information in finder with cmd-i and change it back to users.js.erb.

Categories

Resources