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.
First of all add ‘jquery-rails’ gem to your Gemfile:
Then run
bundle installandrails g jquery:install. That’s all. Now you can work with jQuery.Add a div into your view, for example:
Now you can include some javascript this way:
More about $.ajax: http://api.jquery.com/jQuery.ajax/ (you can use either $.post or $.get or $.getScript)
UPD
working with ajax through
link_toFROM 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 ajaxappcd ajaxapprails g scaffold Comment body:textrake db:createrake db:migrateadd gem 'jquery-rails' to Gemfile=> bundle install => rails g jquery:installlet’s edit comments/index.html.erb:
_comment partial:
Now we will add app/views/create.js.erb file:
comments_controller create action:
that’s all. Now run
rails s. Go tolocalhost:3000/comments.