Ruby on Rails renderin a view with static script files - javascript

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.

Related

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.

Rails production asset compress and integration

Fast Example,
lets say, i have this js file, test.coffee
alert 'test!'
my goal is,
I dont want this code to be loaded in every pages
so i manually included where i want,
<%= javascript_include_tag 'test'%>
but the tragedy happens in production mode,
this test.coffee is not minified in production mode!
I want this code to be minified, but it should not integrate and minified to application.js, because i don't want this code be loaded in every pages.
How can i solve this dilemma?
You are having this issue because rails by default doesn't precompile file with the coffee extension. You should be able to solve this issue be prepending the .js extension so your file should look like this:
test.js.coffee
Straight from the rails docs:
When using asset precompilation, you will need to ensure that your controller assets will be precompiled when loading them on a per page basis. By default .coffee and .scss files will not be precompiled on their own. See Precompiling Assets for more information on how precompiling works.
Note:
There's a mechanism in rails that allows you to "inject" javascript at runtime.
Typically you would define a yield :javascripts in your app layout.
And add content to this yield using:
<% content_for :javascripts do %>
<%= script_tag :test %> # Content here will be yielded
<% end %>
This allows you to define your javascript at the bottom page while injecting page specific assets.

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

javascript_include_tag not working

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.

(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