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:
- installed jquery-rails gem, deleted the public/javascript/rails.js
file - added the line, “gem ‘jquery-rails’, ‘>= 0.2.6’” to my Gemfile, ran
“rails generate jquery:install” - 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
- 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”);
});
})
- the view, app/views/parts/index.html.erb has deletion links for
each part item:
<% if @parts != nil %>
Id | Title | Brand new? | Manage |
---|