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 3976800
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:50:52+00:00 2026-05-20T04:50:52+00:00

I would like to use Ajax in my Ruby on Rails 3 application. Problem

  • 0

I would like to use Ajax in my Ruby on Rails 3 application. Problem is I want the Ajax link to surround a div tag. If possible I’d rather it be with jQuery rather than prototype. How would I accomplish this?

I would like a solution involving some rails type programing something like link_to.

  • 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-20T04:50:53+00:00Added an answer on May 20, 2026 at 4:50 am

    First of all add ‘jquery-rails’ gem to your Gemfile:

    gem 'jquery-rails'
    

    Then run bundle install and rails g jquery:install. That’s all. Now you can work with jQuery.

    Add a div into your view, for example:

    <div id='ajax_div'><%= @article.title %></div>
    

    Now you can include some javascript this way:

    $(
      function(){
        $("#ajax_div").click(function(){
          $.ajax(...)
        }
      }
    )
    

    More about $.ajax: http://api.jquery.com/jQuery.ajax/ (you can use either $.post or $.get or $.getScript)

    UPD

    working with ajax through link_to

    <%= link_to some_path, :remote => true do %>
      <div id='ajax_div'><%= @article.title %></div>
    <% end %>
    

    FROM THE BEGINNING

    Let’s create new rails application. From scratch. To show how ajax works. I won’t test it, so here can be misstypes and mistakes.

    • rails new ajaxapp
    • cd ajaxapp
    • rails g scaffold Comment body:text
    • rake db:create
    • rake db:migrate
    • add gem 'jquery-rails' to Gemfile => bundle install => rails g jquery:install

    let’s edit comments/index.html.erb:

    <h1>Listing comments</h1>
    
    <%= link_to comments_path, :remote => true, :method => :post do %>
      <div>Click me to add random comment</div>
    <% end -%>
    <div id='comments'>
      <%= render @comments %>
    </div>
    

    _comment partial:

    <p>
      <%= comment.body %>
    </p>
    

    Now we will add app/views/create.js.erb file:

    $("#comments").append("<%= escape_javascript( render @comment ) %>")
    

    comments_controller create action:

    def create
      @comment = Comment.new
      @comment.body = (0...50).map{ ('a'..'z').to_a.<<(" ")[rand(27)] }.join
      @comment.save
    end
    

    that’s all. Now run rails s. Go to localhost:3000/comments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to rails and would like to use AJAX to update the a
I would like to use the HTML5 window.history feature to implement full AJAX navigation
I want to reduce postback in one of my application page and use ajax
I would like to implement an AJAX autocomplete search feature of my Ruby on
I would like to use the Google AJAX Feed API to fetch multiple newsfeeds
I am using Ruby on Rails 3 and I would like to update an
I am using Ruby on Rails v3.0.9 and jQuery v1.6.1. I would like to
If I use JQuery to add animation to my Ruby on Rails web application,
I have an array of Integers in Java, I would like use only a
I would like to use R to extract the speaker out of scripts formatted

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.