Topic: Referencing javascript in a form

This seems like a simple, basic question but I can't find a reference that helps.I just want to load a custom javascript file with a form.I don't want every page to load the javascript, so I tried to load it with a javascript_include_tag in the form itself, which I didn't expect to work since it is called for in the html <head> section, and indeed I got a parse error.So I tried to load it in the <head> area of application.html.erb, as in:<head>....<%= javascript_include_tag "tc_cards.js" # => <script type="text/javascript" src="/javascripts/tc_cards.js"></script> %>

However, this resulted in a parsing error with messages like:
unknown regex options...
unmatched )

Any idea how to load javascript into a specific form only, or do I need to insert the script directly into _form.html.erb?

Thanks in advance for any help.  -Fred

Last edited by fredrated (2011-11-02 14:32:55)

Re: Referencing javascript in a form

You can put JavaScript directly into any HTML doc,  either directly,  or putting the JavaScript in a separate file and including it.

<head>
  <%= javascript_include_tag "jrails.js" %>
  <script type="text/javascript">
    $(document).ready(function() {
       blah blah
    }  
  </script>
</head>
Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Referencing javascript in a form

Took me a while to get back to this problem after other issues, but what I don't understand is where to put the <head>  ...  </head> tags. with the javascript reference.  None of the default templates, new.html.erb, edit.html.erb, _form.html.erb etc. have a head section, and when I add one, for example, to _form.html.erb, the page ends up with 2 <head> sections, the second ignored by the browser.
The only template with a <head> section is application.html.erb, and putting it in there presumably includes it in all pages created by my application, something I was trying to avoid.

Re: Referencing javascript in a form

I would stick it layouts/application.html.erb for now, just to get it working,  won't do any harm.  Then once you've started to built up a bunch of JavaScript you'd start putting it (the stuff inside the <script> tag, into separate .js files and include them with <%= include_javascript_tag "your.js" %>  in the views that need them.

Also note that it's OK to have multiple $(document).ready's in any one HTML page.  So you can have initialization code that is universal to your entire app in layouts/application.html.erb,  and initialization specific to a certain view,  or all views based on a specific controller.

If you are on Rails 3.1 or greater, be sure to check into the Asset pipeline stuff,  as there are slick new ways to get your JavaScript automatically minified,  and arranged to be included based on controller, etc. AND served up by the WEB server directly.

Last edited by BradHodges (2011-12-21 14:28:06)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Referencing javascript in a form

Hold ON,  I replied to the wrong POST,  I had just posted on the same topic,  I didn't notice your's was an old post!!!

Sorry,  let me have another look

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Referencing javascript in a form

Well strangely enough, even though I thought your latest post was for a completely different topic,  it was strangely appropriate.

The only thing you have to understand is the Javascript doesn't HAVE to be in the <head> section of the page.

You can put raw <script type="text/javascript">

or

<%= javascript_include_tag "your.js" %>

anywhere in your page

Last edited by BradHodges (2011-12-21 20:17:02)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.