Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5984509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:23:11+00:00 2026-05-22T22:23:11+00:00

I am using Ruby on Rails 3 and I would like to use jQuery

  • 0

I am using Ruby on Rails 3 and I would like to use jQuery instead of Prototype (this one is what I am using at this time). I read many books, guides and tutorials but I still don’t understand how I must use jQuery in my code (maybe it seams that nobody it “really” clear on this matter). Also, I have a lot of code written using Prototype so I would like to handle both at the same time and “progressively” migrate to jQuery.

I included the jQuery file in the layout file making <%= javascript_include_tag :defaults, 'jquery' %> so that the <script src="/javascripts/jquery.js?1307515315" type="text/javascript"></script> is present in the source code of all HTML pages. In order to avoid errors in the application.js file I added jQuery.noConflict() to the application.js file. Now, what I must do?

If I state a jQuery method in the application.js file

jQuery(document).ready(function() {
    jQuery('#test_id2').click(function() {
      alert('Handler for .click() called.');
    });
})

and I call it using

<%= link_to 'change1', '#', :id => 'test_id1' %>

it will work but I would like to put the above code in a separated js file. How can I do and maj?

How I must name the JS file for a controller action? Where (in which folder) I must locate the related JS file? How can I state the respond_to method in my controllers?

def edit
  ...
  respond_to do |format|
    format.html # render edit.html.erb
    format.js # render... what file?! where?!
  end
end

Using Prototype I can write code directly in view files like the following:

<%= link_to_function( "change", :id => 'test_id2' ) do |page|
  page[:test_id3].toggle
  ...
end %>

How can I do that using jQuery in view files?

Have you some advice?


UPDATE

In my edit.js.erb file I have

<%
jQuery(document).ready(function() {
    jQuery('#test_id1').click(function() {
      alert('Handler for .click() called.');
    });
})
%>

In the edit.html.erb file I have

<%= link_to 'Test link', '#', :id => 'test_id1' %>

But when I click the above link jQuery doesn’t work (the alert method is not triggered). Furthermore, if I try to insert the JavaScript code directly in the view file, I get the following error:

NameError: undefined local variable or method `document' for #<#<Class:0x00000102aa04d0>:0x00000102a75eb0>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:23:12+00:00Added an answer on May 22, 2026 at 10:23 pm

    There are two differents things.

    They are regular script that you can load the same way as you did with jquery:
    You put them in your public/javascript directories and include them with

    <%= javascript_include_tag YourNameFile %>
    

    Note that I didn’t add the .js. javascript_include_tag will do that for you.

    Then you have JS Templates. It’s like your Prototype views. Except you have Jquery inside.
    So what you previously did works!
    If you use

    respond_to do |format|
      format.html # render edit.html.erb
      format.js # render edit.js.erb
    end
    

    It’ll render edit.js.erb (you can find it in the same directory as your edit.html.erb file).

    Then you can add your jQuery AND your ‘ruby’ in those files (exactly like prototype).
    Just make sure that you load your jQuery.

    If I missed something, add comment. 🙂

    === EDIT ===

    In your Templates, you have to put <% %> only arounds Rails function !! Otherwise, it’s supposed to be Javascript. So you edit.js.erb should looks like:

    jQuery(document).ready(function() {
        jQuery('#test_id1').click(function() {
          alert('Handler for .click() called.');
        });
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ruby on Rails 3 and I would like to use a
I would like to build a simple REST web service (using Ruby on Rails).
I am using Ruby on Rails 3 and I would like to handle user
I am using Ruby on Rails 3 and I would like to handle properly
I am using Ruby on Rails 3 and I would like to validate a
I am using Ruby on Rails 3 and I would like to change the
I am using Ruby on Rails and I would like to make HTTP requests
I am using Ruby on Rails 3 and I would like to set some
I am using Ruby on Rails 3 and I would like to know in
I am using some nested layouts in Ruby on Rails, and in one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.