Thursday, 15 September 2011

Confused about how to use vanilla JavaScript with Ruby on Rails -



Confused about how to use vanilla JavaScript with Ruby on Rails -

i have 2 views, 1 called dashboard , other called home, both have respective index.htm.erb files. corresponding javascript files in assets/javascripts folder not seem map these views. example, when run alert in home.js , navigate dashboard.html.erb alert still runs.

how remedy functionality looking for?

i confused how write page specific raw javascript in rails.

edit: utilize public folder had ancillary issues , hence why started using files in assets folder.

one of rails' "opinions" javascript should concatenated single file (application.js), minified, , served client. effort minimize number of requests client needs create when accessing application (the goal have three, 1 html, 1 css, , 1 javascript [excluding whatever images may on page.])

if in application.js, you'll see note saying 'manifest' file, , line looks like

require_tree .

which says "load every javascript file in folder file"

in order load page-specific javascript, you'd need

put separate javascript file app/assets/javascripts [call custom.js, say] stub out loading file application manifest writing stub custom in application.js include custom javascript manually in view (or, more improve probably, layout renders view): <%= javascript_include_tag 'custom' %>

however, i'd encourage @ whether need separate javascript, or whether it's problem can solved localizing script page(s) it's intended for, maintain same functionality , maintain loads times ever-so-slightly faster.

$('body.some_custom_class').ready(function() { alert('i'm running on page!'); });

javascript ruby-on-rails

No comments:

Post a Comment