1

I am using :

Linux Ubuntu 16.04

ruby 2.4.0p0;

rails 5.1.1

I am a begginner in rails and I have followed the Ruby on Rails 4 Essential Training in lynda.com in order to learn. I am now stuck at the loading js assets task.

I have created the "app/assets/javascripts/demo.js" file which contains the js function :

function jsRoar(name) {
  alert('I am ' + name + '. Hear me roar!');
}

Then I have load this file in my manifest file "app/assets/javascripts/public.js"

//= require jquery
//= require jquery_ujs
//= require demo

After that I have added external include of js to my layout at "app/views/layouts/application.html.erb":

<%= javascript_include_tag "public" %>

In my controller I have included my layout by typing at "app/controllers/demo_controller.rb"

layout 'application'

And finally I have called my function by typing these js codes in my demo/index.html.erb :

<%= link_to('Roar', '#', :onclick => "jsRoar('Yassser');return true;") %>   
<%= javascript_tag("jsRoar('Rails');")%>

After that i get "undefined function" in my chrome console and the alert doesn't appears .I have defined the function in the same html.erb and it works so it's not a problem of javascript i think. I must mention also that, at first when i run my server, i have obtained a problem of assets compilation so I have fixed by adding this line in "config/initializers/assets.rb":

Rails.application.config.assets.precompile += %w( public.js)

I am available for more details. Please help.

  • Does clicking the link after everything finishes loading produce the popup? – Simple Lime Jul 11 '17 at 15:15
  • Can you look at the `public.js` included in your page (that is, as the browser sees it, after Rails has post-processed it) and verify that `demo.js` was included in it? My guess is that there's something about the syntax of your manifest file which is keeping it from being included. – pjmorse Jul 11 '17 at 15:19
  • @SimpleLime : no the popup dosen't shows up after clicking the link neither after loading page as I made a call after the page loading and a call after clicking the link. – Yasser Kotrsi Jul 11 '17 at 15:27
  • @pjmorse: this is the content of my public.js, I don't see anything wrong : //= require jquery //= require jquery_ujs //= require demo – – Yasser Kotrsi Jul 11 '17 at 15:28
  • @YasserKotrsi Is that what the browser is seeing, though? That is, if you load the page in your browser, then use Developer Tools or the like to check public.js, does it include JS code, or that manifest copy? If it's showing that manifest, the problem is that your JS isn't getting post-processed. – pjmorse Jul 11 '17 at 15:31
  • @pjmorse : okey i understood your question. My browser dosen't see the script . In fact, I used the the firefox "firebug inspector" and it tells me that there are errors "ReferenceError: jsRoar is not defined". I also checked the head of the page in the inspector and here what i found : ` ` – Yasser Kotrsi Jul 11 '17 at 16:21
  • @YasserKotrsi can you try and add `demo.js` to the precompile as well? I think it ought to raise an error if it wasn't precompiling and being required but who knows -> `Rails.application.config.assets.precompile += %w( public.js demo.js )` – m3characters Jul 11 '17 at 16:52
  • @MicaelNussbaumer I already try this solution but still unfixable sadly. – Yasser Kotrsi Jul 11 '17 at 18:27

3 Answers3

2

My problem was resolved after deleting the "demo.coffee" file. I discovered that the my issue is derived from the fact that I have two files with the same name (but different extensions) "demo.js" and "demo.coffee". Since the Rails asset helpers treat coffescript and js files equally this is ambiguous. So I have deleted the "demo.coffee" and all my problems are settled. Thank you all mates. Great thnx for @max with his reply in this post

0

I think this:

<%= javascript_tag("jsRoar('Rails');")%>

should be:

<%= javascript_include_tag("demo") %>
m3characters
  • 2,240
  • 2
  • 14
  • 18
  • Why? The `javascript_include_tag("public")` should have loaded the function, because the `public` manifest already requires `demo`. I'm not saying you're wrong, but I don't understand from your answer why you would be right. – pjmorse Jul 11 '17 at 15:16
  • Actually, I don't think this is the problem, because through the `<%= javascript_tag( "jsRoar('Rails');" )%>` helper i intended to call the function. And for the js resource loading I already did in the layout by adding : `<%= javascript_include_tag("public") %>` in the header section as the public.js is the manifest file of this project. – Yasser Kotrsi Jul 11 '17 at 15:19
  • I just tried it with the difference being the `requires` included in `application.js` and it worked as expected so I would think that pjmorse is correct and the js file is not being rendered correctly. And you don't need to call `layout 'application'` unless your controller is inheriting from some other controller who has a layout different than application, because by default it uses `'application'` – m3characters Jul 11 '17 at 16:12
0

See if this code works in your view:

$(document).ready(function(){
   jsRoar('Yassser');
})
Alex C
  • 1,334
  • 2
  • 18
  • 41