开发者

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.

  1. http://asciicasts.com/episodes/205-unobtrusive-javascript

  2. http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜