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"