How to convert existing rjs to use jQuery and json in Rails 3?
Right now in my Rails 3 app I'm rendering partials using rjs in my controllers. For example, when saving a new item to a table, I refresh the table:
respond_to do |format|
format.js
{
render :update do |page|
page["#table_d开发者_如何学JAVAiv"].html(render :partial=> 'table');
end
}
end
I'm really stuck on how I would go about keeping the same functionality, but moving away from the use of rjs. How can I accomplish this using jQuery, JSON and unobtrusive javascript? Thanks in advance!
You can remove the respond_to block and have another view with the js.erb extension, named after your action of course.
Taking the 'show' method as an example:
In show.js.erb
$("#table_div").html("<%= escape_javascript(render('table'))%>");
Here's a couple of resources that will help you get acquainted with Unobtrusive JavaScript in Rails 3.
http://asciicasts.com/episodes/205-unobtrusive-javascript
http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/
精彩评论