I've got an issue that must come from my side but turning me crazy :-p
When I'm in development environment, I've to put these lines in application.html.erb to have I18n.js working :
<%= javascript_include_tag "i18n" %>
<%= javascript_include_tag "translations" %>
The problem is that on production environment, it doesn't work (it doesn't found the i18n.js, 404, I know that's normal, due to the assets pipeline), I've to put these lines in application.js :
//= require i18n
//= require i18n/translations
But if I leave these like that, I've got an error in development environment : uninitialized constant SimplesIdeias
It works for all the rest of my scripts, only i18n.js got a problem to load.
What is the way to make them both works ? :p
Related
currently I am working on Rails 6, and I met
"cannot load such file -- coffee_script"
I took a look in Gemfile and there is no gem 'coffee-rails'
and I have tried $ rails tmp:cache:clear to clear cache and restart server.
but I still get the same error.
!!!
%html
%head
%meta{content: "text/html; charset=UTF-8", "http-equiv": "Content-Type"}/
%title Myapp
= csrf_meta_tags
= csp_meta_tag
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'application'
%body
= render 'layouts/notifications'
= yield
enter code here
application.js
//= require jquery
//= require rails-ujs
//= require_tree .
enter image description here
To be able to answer your question better, you should probably also post your application.js file – because this is the thing that triggers the error.
Generally, two things:
Rails doesn't really encourage using CoffeeScript anymore. That doesn't mean that you can't use it – just that it has fallen out of favor and people tend to use other facilities these days (eg. Babel).
In order to make CoffeeScript work in Rails, you need to have the coffee-rails gem in your Gemfile. If it's not there, you need to add it and then it should usually work out-of-the-box.
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.
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
I think this:
<%= javascript_tag("jsRoar('Rails');")%>
should be:
<%= javascript_include_tag("demo") %>
See if this code works in your view:
$(document).ready(function(){
jsRoar('Yassser');
})
I have a rails application and the application.js after asset compilation takes more than 1 MB. This is slowing down my entire site.
I use Apache, Rails 4, jQuery, quite heavy JavaScript and AJAX. I would be very grateful if someone could point me in the right direction.
This may not be feasible in your particular case, but has certainly helped me keep Application.js from bloating.
As I'm sure you know, Application.js compiles all specified files (by default, all of them) into a single .js file, which is loaded (again, by default) as part of your layout in every page. Often times this results in the inclusion of entirely unnecessary custom scripts loading in every page, and slowing down the entire application. I personally find this behavior undesirable. What I find works for my sites is only including my "core" javascript components in Application.js (jquery, bootstrap's js libraries, and any scripts that pertain to layout.html.erb itself), and specifying the rest in the pages that need them. For example:
application.js
Note that it does NOT include require tree .. This is important, as that is the line which specifies the inclusion of the entire assets/javascripts folder. "Template" in this case is the .js file a defined which pertains to layout.html.erb
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require bootstrap-sprockets
//= require template
//= require turbolinks
layout.html.erb
The following is the very end of my layout, immediately before the closing body tag. This loads application.js on every page, and after that loads any js specified in the view.
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<% if content_for?(:javascript) %>
<%= yield :javascript%>
<% end %>
The View(s)
In any view that requires page-specific javascript, you may specify the files with a Rails javascript helper
<% content_for :javascript do %>
<%= javascript_include_tag 'pages/profile', 'data-turbolinks-track' => true %>
<% end %>
initializers/assets.rb
Finally, make sure that your scripts are still being precompiled, even though they aren't a part of Application.js.
Rails.application.config.assets.precompile += %w( pages/profile.js )
...or, more efficiently assuming you have many pages with their own scripts...
Rails.application.config.assets.precompile += %w( pages/* )
In Conclusion
I find this technique really helps keep the size of Application.js down, and makes for good practice in general. I hope you find it useful, and apologize if it is extraneous to your problem.
Have you ever thought about using the CDN hosted jQuery Version? Could you provide your uncompiled application.js.
You could also try to use browserify or require.js
I have a problem with Rails and Jquery. Im using AJAX to add comments to articles without reloading them. The following code got automatically included in my views/application.html:
<%= javascript_include_tag "application" %>
<%= javascript_include_tag :all %>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" %>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" %>
Everything seemed to work fine, until i realised that the server console shows the following error:
ActionController::RoutingError (No route matches [GET] "assets/all.js")
So since this line doesn't seem to add anything to the application other than an error i deleted it. Next time I started the server and used the application all of a sudden every comment gets posted twice!? Otherwise everything still worked fine. So I added the deleted line again and I have no idea why but when I add the line
<%= javascript_include_tag :all %>
again everythings works fine again, only one comment gets posted as intended. However I dont want to keep this in the code since it throws an error. Can someone explain this behaviour and tell me how to fix this?
Rails 3.1 uses sprockets to bundle javascript and css files. This makes the :all option deprecated. Sprockets use 'magical' comments to manage which javascripts are included.
So your application.js should look something like this:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .
The first line also includes jQuery itself, so you don't need the other script tags. All you need to to is to point to application.
<%= javascript_include_tag "application" %>
If you're deploying to production, you'll need to run rake assets:precompile.
There is a Railscasts episode on assets, which is a must see.
So I've recently started to include quite a few .js files in to my web application and some of them depend on each other so load order is important.
However rails caching just seems to load a all.js file with no particular order to the files.
What's a good way to solve this issue?
You can do as follows
First, load the default JavaScript files.
Then load other scripts in the order that you want
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag "script_1", "script_2", "script_3" %>
The load order depends on your Javascript manifest file. In Rails 3.1 you can go to
app/assets/javascripts/application.js
At the bottom of the file you will see directives for rails how what / how to include files into the Rake pipeline. In the below example, I included a new directive that will include all the files in the directory called "Templates". I also made sure that the Handlebars.js templating file is called before all of the files in the "Templates" directory, otherwise the browser would throw an exception
//= require handlebars
//= require_tree ../templates
//= require_tree .
Hope it helps!
I've been experimenting with the YUILoader Module, it seems pretty nifty, though I am currently frussing about loading up custom modules. It's totally doable, I just couldn't figure it out in 5 mins.
http://developer.yahoo.com/yui/yuiloader/ (YUI2.8.1)
http://developer.yahoo.com/yui/examples/yuiloader/index.html