Hi, guys,

 I'm in the midst of moving an app from rails 2.3.8 to rails 3.0.9.

Sadly, rails 3 no longer has javascript generators and I'm now forced
to do more javascript.

For my project, I have selected jQuery as the javascript framework for
my rails 3.0.9 app.

What I have done to have my app's deletion link (for each item)
trigger an alert box when the deletion process is completed:
1) installed jquery-rails gem,  deleted the public/javascript/rails.js
file
2) added the line, "gem 'jquery-rails', '>= 0.2.6'" to my Gemfile, ran
"rails generate jquery:install"
3) set up the controller action, destroy to respond by giving a json
object when it's being called by ajax


  # DELETE /parts/1
  # DELETE /parts/1.xml
  def destroy
    @part = Part.find(params[:id])
    @part.destroy

    respond_to do |format|
      format.html { redirect_to(parts_url) }
      format.xml  { head :ok }
    format.js   {
      render :json => {:name => 'John'}
    }
    end
  end


4) added the following to application.js

// Place your application-specific JavaScript functions and classes
here
// This file is automatically included by
javascript_include_tag :defaults

$(document).ready(function () {
  function(e, data, textStatus, jqXHR){
   alert(data.name + ' has been deleted');
        $('a[data-method="delete"]').css("color", "red");
  });
})


5) the view, app/views/parts/index.html.erb has deletion links for
each part item:

<% if @parts != nil %>
<table border="0" id="table-lined">
  <tr>
    <th>Id</th>
    <th>Title</th>
    <th>Brand new?</th>
    <th colspan="5">Manage</th>
  </tr>

    <% @parts.each do |part| %>
      <tr>
        <td>
            <%= part.id %>
        </td>
        <td><%= link_to(part.title, part) %> </td>
        <td><%= link_to 'Preview', part %></td>
        <td><%= link_to 'Update', edit_part_path(part) %></td>
        <td>
            <%=
                link_to 'Delete',
                    part_path(part.id),
                    'data-method' => 'delete',
                    'data-confirm' => 'Are you sure?',
                    'data-remote' => 'true',
                    'class' => 'delete_part'
            %>
        </td>
      </tr>
    <% end %>
    </table>
    <hr>
<% end %>

When I run "script/rails server"
====================
Pre: I turn on Firebug on Firefox
1) I clicked on the "delete" link on an entry. Confirm dialog box pops
up. I click "yes"
2) I looked at the "Net" tab and I see the DELETE request. Clicked on
the DELETE tab
3) Looked at the header and json sub tabs and I see the expected data
that I defined to return from the destroy action in the parts
controller (above)

What am I missing?

The json data gets returned but somehow, jquery is not intercepting
it :(
Is the ajax callback that I put in application.js errorneous/
incomplete?

Any good references (that work) with rails 3 UJS (ajax)?



My references were:
1) 
http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/comment-page-1/#comment-377817
2) http://rails3stuff.posterous.com/#!/59899595
3) http://docs.jquery.com/Ajax_Events
4) http://railscasts.com/episodes/205-unobtrusive-javascript
5 ) 
http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/#comment-38259
6) http://chrissloan.info/blog/rails_3_jquery_ujs/
7) http://railsdog.com/blog/2011/02/28/callbacks-in-jquery-ujs-in-rails3/



What I have also tried:
------------------------
 Even tried defining app/views/parts/destroy.js.erb -  with a simple,
alert(' Calling destroy.js.erb ')   - Does not work :(






Please.

Thank you



-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to