Solved

Jquery with rails 3

Posted on 2010-11-10
3
941 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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 code hide show and toggles. 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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now