Solved

Jquery with rails 3

Posted on 2010-11-10
3
943 Views
Last Modified: 2012-05-10
this jquery code works when referenced internally but when i place it in the application.js file. it dosen't work
working when referenced like this:



##application.html.erb

<script type="text/javascript">  

	$(function () {

		setInterval("rotateImages()", 2000);



	});



	function rotateImages () {

		var oCurPhoto = $("#slideshow div.current");

		var oNxtPhoto = oCurPhoto.next();

		if (oNxtPhoto.length == 0)

			oNxtPhoto = $("#slideshow div:first");



		oCurPhoto.removeClass('current').addClass('previous');

		oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({ opacity:1.0 }, 1000, 

			function () {

				oCurPhoto.removeClass('previous');

			});	

	}





but not like this:



##application.html.erb

	<%= javascript_include_tag "application" %>

  <%= javascript_include_tag "jquery-1.4.3" %>

	<%= javascript_include_tag "rails" %>

  <%= csrf_meta_tag %>



##application.js

(function () {

	setInterval("rotateImages()", 2000);



});



function rotateImages () {

	var oCurPhoto = $("#slideshow div.current");

	var oNxtPhoto = oCurPhoto.next();

	if (oNxtPhoto.length == 0)

		oNxtPhoto = $("#slideshow div:first");



	oCurPhoto.removeClass('current').addClass('previous');

	oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({ opacity:1.0 }, 1000, 

		function () {

			oCurPhoto.removeClass('previous');

		});	

}

Open in new window

0
Comment
Question by:depassion
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
kristinalim earned 500 total points
ID: 34104211
Try moving your include tag for application.js after the include tag for the jQuery library. The problem with your situation seems to be that when your application.js is loaded, the dollar sign ($) is not yet defined, something that the jQuery library takes care of.
0
 
LVL 12

Expert Comment

by:JESii
ID: 34104375
I believe that's partially correct; but the standard Rails JS library (Prototype) also uses the $ tag, so I think you have to also tell jQuery to redefine the $ tag to something else, like this:
  jQuery.noConflict();
That will change the jQuery $ variable to jQuery

See http://api.jquery.com/jQuery.noConflict/ for more information.
0
 
LVL 4

Expert Comment

by:kristinalim
ID: 34104510
Definitely as JESii said, you would need jQuery.noConflict() if you also include the Prototype library.

By default the Prototype library is included in the Javascript expansion :defaults, so you need this if have something like:

<%= javascript_include_tag :defaults %>

or:

<%= javascript_include_tag "prototype" %>

in your layout.
0

Featured Post

Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

WHY MVC is the future technology... As many of you know, MVC is Model,View,Controller pattern. Model View Controller pattern was invented in a Smalltalk context at Xerox in late 1970s. Why is this buzz about MVC: --------------------------- …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now