4

This will sound like a stupid question for most but I'm really having problems with adding JavaScript files (such as jquery) to my application.html.erb, if I have the jquery first then the rails JavaScript doesn't work correctly. what's the best way to include all JavaScript files to the application.html.erb from my JavaScript folder?

Edit: how can I have Jquery and Prototype run side by said in my rails 3 application? what should my application.html.erb code look like?

2 Answers 2

2

If you want to use jQuery and Prototype side-by-side, you should order your javascript include statements as follows:

<%= javascript_include_tag :defaults, "jquery.min", "jquery-ui.min" %>
<script type="text/javascript">
  jQuery.noConflict();
</script>

Note: After doing this, you will not be able to access any jQuery methods using $(...). Instead, you'll need to use jQuery(...)

However, if you don't need Prototype, delete your prototype.js file and install the proper jQuery-specific rails.js file (the one that Rails uses by default is Prototype-specific). See https://github.com/rails/jquery-ujs for instructions.

Sign up to request clarification or add additional context in comments.

6 Comments

can you please include the code i should have? I want them to both run side by side, i have: ` <%= jQuery.noConflict(); %> <%= javascript_include_tag "jquery.min", "jquery-ui.min" %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %>` but it wont work, jquery wont work
The jQuery.noConflict() statement should be after you include jQuery, but before you include everything else.
Im so sorry for wasting your time, but im really having problems making it work. Can you just add code for what it should like? im relatively new to rails. when i put the statement i get an error saying : undefined local variable or method `jQuery'
Thank you @dmarkow I want them to run side by side, I tired your solution but it still wont work. its not recognizing the getting: $(link).prev is not a function in firebug. this is driving me mad.
That's because $ is used by Prototype, and they can't both use it. Calling jQuery.noConflict() means that you have to replace $ with jQuery when you want to use it. So try jQuery(link).prev instead.
|
2
  1. Create new project skip prototype with -J:

    rails new -J test -d mysql

  2. Modify the Gemfile, add JQuery:

    gem 'jquery-rails'

  3. Bundle update:

    bundle update rails

  4. Generator the JQuery, --ui means withe JQuery UI:

    rails generate jquery:install --ui

  5. Modify the config/application.rb:

    config.action_view.javascript_expansions[:defaults] = %w(jquery jquery-ui rails application)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.