bytebiscuit
bytebiscuit

Reputation: 3496

ajax and rails 3.x

i'm trying to update a div in my index.html.erb file using Ajax in a rails app, but at this point i'm totally confused and totally frustrated by the lack of good explained tutorials(as far as i'm aware) on the web.

I currently have an index.html.erb file which amongst other things has the following block:

<table  border="1">
<% @dbs.each do |db| %>
<tr>
    <td ><%= link_to db, :action => :show, :db => db,  :remote => true %></td>
</tr>
<% end %>
</table>
<div id="collections">

</div>

my index controller just initliazies the @dbs var there and that's it, it doesn't have any respond_to blocks. I want to update the #collections div, but I'm struggling to understand how to go about doing this?

UPDATE - controller class:

Upvotes: 0

Views: 87

Answers (2)

Dougui
Dougui

Reputation: 7230

You can create show.js.erb with this code :

comment_partial = '<%= escape_javascript(
  render('my_partial')
) %>';
$('collection').html(comment_partial);

Obviously, to use a partial is not mandatory. It's just JS with JQuery.

You should replace this code :

respond_to do |format|
  format.js
end

By this :

render :format => :js

Upvotes: 1

yngccc
yngccc

Reputation: 5694

Make a show.js.erb file in the view folder for your controller, and write js code like

$("#collections").dostuff();

Upvotes: 0

Related Questions