javascript_include_tag not working - javascript

i'm using a javascript_include_tag in one of my 'partial' rhtml files. yet, when the page loads, I get an error in firebug saying that the class included in the javascript file was not defined. Basically, the javascript file is not getting added. I have kept the js file in the public/javascripts folder
Please help

First, what version of rails are you running? I know with my version of rails(3.1.1) the javascript_include_tag defaults to the app/assets/javascript directory not the public/javascripts folder. Placing files in the assets/javascript folder should allow you to properly link to them in a similar fashion.
This will include all js files residing in the assets/javascript folder and should already be at the top of your views/layout/application.html.erb file.
<%= javascript_include_tag "application" %>
Also any subdirectories will be included by the above tag. Hope this helps.

Related

Ruby on Rails renderin a view with static script files

In my ruby on rails application I have controlled called AnimalsController. At this controller I have a method "edit" and a corresponded edit.html.erb view in my animals folder. When I also manually added some static javascript file into the page which are located at public/assets/js/ folder. When I render the view, instead of rails is trying to load these javascripts file in the relative path instead of mydomail.com/assets/js/ folder.
Url:
/animals/:id/edit
I have script file myscript.js inside assets/js/
When I send request to htt://0.0.0.0:3000/animals/:id/edit
Rails search for http://0.0.0.0:3000/animals/:id/edit/assets/js/myscript.js
I want it to seach the script at http://0.0.0.0:3000/animals/js/myscript.js
This is code is in my edit.html.erb
<script src="assets/js/myscript.js"></script>
Not: I intentionally don't use <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
Any suggestions ?
Thanks.
It should be
<script src="/assets/js/myscript.js"></script>
Without the starting '/' it assumes the asset path is rooted on the page path.
With the starting '/' it assumes the asset path is rooted at the domain.
If you use the rails helper, it will handle this for you.
<%= javascript_include_tag 'myscript' %>
Just don't forget to add this file to the assets initializer config/initializers/assets.rb.

Rails asset pipeline require from public folder?

Is this a good practice to require assets located under public folder? e.g. I have lots of javascript under public/mythirdparyjs folder and I want to make these javascript files available for only specific page, what will be the best way to do this?
Thirdparty js files should be in your vendor folder. Once you precompile them for production they will be then moved into your public folder automagically. To call certain files on specific pages you need to remove the require_tree directive and also pay attention to which files are called in your application.js asset file. At the bottom of every view page (html.erb) you can add the following to run only page specific js:
<%= content_for :javascript do %>
<script type='text/javascript'>
YOUR FUNCTIONS HERE
</script>
<% end %>
Then in your application/layout view at the bottom before the closing body tag you should add:
<%= javascript_include_tag params[:controller] if Rails.application.assets_manifest.assets["#{params[:controller]}.js"] %>
<%=yield :javascript %>
This will then run any js on a page by page view basis and include any require statements for any files in the corresponding controller's js asset file.
UPDATE to address comment: If you have a gem you installed and then need to require certain js files, these should be required from your app/assets/javascripts folder in either the application.js file (if you want that gem to be usable throughout your application) or in the controller specific js file if you only need that functionality on a few pages. For example, the angular gem is probably used throughout your application so you would add the //=require statement to the application.js file in your app/assets/javascripts folder. If you manually download some js libraries you should put them in the vendor/javascripts folder however you still require them in your app/assets/javascripts folder files wherever needed.

Loading JavaScript File With Rails

So running Rails 3.2. In my assets/javascripts folder I have the file "main.js.erb". The contents of this file are as follows:
$(function(){
$(document).load(alert("Hello"))
})
My application.js file is just the default generated after an install initiated with 'rails new'.
Any ideas as to why the load event is not being triggered?
Since your application.js is just the default, you probably haven't included the file for usage.
Following are different ways you can include the file:
In app/assets/javscripts/application.js
//= require main
In your template file or layout file eg. app/views/layouts/application.html.erb
<%= javascript_include_tag "main" %>
For further details please refer to The Asset Pipeline RailsGuides.

javascript_include_tag and stylesheet_link_tag

I would like to know if there is some standard (Rails-version-independent) folder in Rails application where to put javascripts or stylesheets.
when I insert javascript_include_tag and stylesheet_link_tag they link to /assets by default. How to specify folder, containing javascripts or folder containing stylesheets?
I read that there is some Rails.application.config.assets.paths configuration thing which lets me to specify folder for assets (btw. in which .rb file should I assign its value?). But it seems to point out a single folder for all JS and CSS files together, but I want to store them in separate folders.
For rails 3 you can put all your javascripts and CSS files in public/assets/javascripts and public/assets/stylesheets
javascript_include_tag will be linked correctly in that case.
For rails 2 the same, just leave assets out.
those tags are helpers, so they point to the asset folder and respectively the folder /javascripts or /stylesheets.
<%= javascript_include_tag "something" %>
generates a link to /assets/javascripts/something.js
You can edit the path in config/application.rb just place
config.assets.enabled = true
config.assets.paths << "#{Rails.root}/app/or_whatever_path_to_use"
in there

(rails) how does one correctly link to a javascript file in a rails view?

Is there a helper of some sort? I want to make a javascript file, put it in the rails directories somewhere, and have the file be included in a view so I can use the methods and functions I declare in the file.
<%= javascript_include_tag "jquery" %>
With jquery.js in public/javascripts

Categories

Resources