Forum: Ruby on Rails jRails problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
César D. (Guest)
on 2009-05-08 15:45
Hi,

I am developing a Rails app and now I want to try some Ajax
funcionality. I have a tracks list, and each track has a delete button.
I have this operation implemented without Ajax with a redirection, and
now I want to do it with Ajax. I am using JQuery and jRails.

Here is my code:

This is the delete function in my controller:

def delete_track
    @track_id = params[:track_id]
    Track.delete(@track_id)
    respond_to do |format|
      format.html {redirect_to :action => 'index'}
      format.js
end

This is my html where the tracks list is:

<div id="musicalUnit">
  <h3 id="musicalUnitTile">Estado de su unidad musical:</h3>
  <ol>
    <%unless @musical_unit.nil? %>
      <% for track in @musical_unit.tracks %>
        <li id="track<%= track.id %>"><%= track.name %>  de <strong><%=
track.album.singer.name %></strong>
          <%= link_to_remote(image_tag("delete.png", :alt => 'Borrar',
:size => "16x16", :title => "Borrar " + track.name + " de mi unidad
musical"),
            :update => "musicalUnitTile", :url => {:controller =>
"tracks", :action => 'delete_track', :track_id => track.id}) %></li>
        <% end %>
      <% end %>
  </ol>
</div>

And this is my RJS code:

page['#track' + @track_id].hide

I want to hide the li element of the track after deleting it from the
db.

In my application.js I have this:

jQuery.ajaxSetup({
    'beforeSend': function(xhr){xhr.setRequestHeader('Accept',
'text/javascript')}
})

The problem is that after executing the delete action, the RJS code is
called, but it is not interpreted. The page prints the RJS as if it was
html code in the  musicalUnitTile div, and I want to execute the RJS
code as Javascript.

What am I doing wrong?

Thanks!
This topic is locked and can not be replied to.