的link_to远程=>真不使用Ajax更新真不、link_to、GT、Ajax

2023-09-10 16:08:18 作者:如初〃

使用rails3和原型(rails.js)

Using rails3 and prototype (rails.js)

我的产品,编辑一个简单的列表,并删除链接的图像。 删除一个产品时,该列表不被更新。刷新页面显示,该产品确实已被删除了。

I have a simple list of products with edit and delete links as images. When deleting a product, the list is not updated. Refreshing the page shows that the product has indeed been deleted.

/app/views/products/list.rhtml

/app/views/products/list.rhtml

<div id="product_list">
  <%= render :partial => 'list'  %> 
</div>

/app/views/products/_list.rhtml

/app/views/products/_list.rhtml

<%= link_to image_tag("delete.png"), { :controller => 'products', :action => 'destroy', :id => product }, :method => :delete, :confirm => "Are you sure?", :remote => true %>

/app/controllers/products.rb

/app/controllers/products.rb

  def destroy
    Product.find(params[:id]).destroy
    @products = Product.all
  end

/app/views/products/destroy.rjs(不知道做什么用的?)

/app/views/products/destroy.rjs (not sure what to do with that...)

$(document).ready(function() {
    $("#product_list").html("<%= escape_javascript( render(:partial => "list") ) %>");
});

因此​​,远程链接似乎做工精细。 我不知道如何使用AJAX回调以更新#product_list

So the remote link seems to work fine. I'm not sure how to use the ajax callback to update #product_list

我试图把在页面头部以下内容:

I tried to put the following in the head of the page:

$(document).ready(function(){
  $('#product_list').bind("ajax:success", function(evt, data, status, xhr){
      alert('hello');
    })
});

但它不会执行(这可能不是一个有效的code的原型),我不知道反正什么code摧毁一个产品后,把里面让我的名单得到更新

But it's not executed (that's probably not a valid code for prototype) and I wouldn't know anyway what code to put inside so that my list gets updated after destroying a product

任何帮助(不是使用jQuery等)大大AP preciated!

Any help (other than "use jQuery") is greatly appreciated!

修改:下面是删除操作的服务器日志(后我感动的JavaScript以上destroy.js.erb)

EDIT: Here is the server log for the delete action (after I moved the javascript above to destroy.js.erb)

Started POST "/products/destroy/3" for 127.0.0.1 at .....
Processing by ProductsController#destroy as JS
Parameters: {"_"=>"", "id"=>"3"}
[1m[36mProduct Load (0.0ms)[0m  [1mSELECT `products`.* FROM `products` WHERE (`products`.`id` = 3) LIMIT 1[0m
  [1m[35mSQL (0.0ms)[0m  BEGIN
  [1m[36mSQL (0.0ms)[0m  [1mDELETE FROM `products` WHERE (`products`.`id` = 3)[0m
  [1m[35mSQL (78.1ms)[0m  COMMIT
  [1m[36mProduct Load (0.0ms)[0m  [1mSELECT `products`.* FROM `products`[0m
Rendered products/destroy.js.erb within layouts/standard (31.2ms)
Completed 200 OK in 312ms (Views: 62.5ms | ActiveRecord: 78.1ms)

处理通过的ProductsController#破坏为JS 的因此远程链接的作品

Processing by ProductsController#destroy as JS so the remote link works

[36mProduct负荷(0.0ms)[0分[1mSELECT 产品。的FROM 产品 *的@products = Product.all执行

[36mProduct Load (0.0ms)[0m [1mSELECT products. FROM products* The @products = Product.all is executed

提炼产品/布局内destroy.js.erb /标​​准的JavaScript的外商投资企业呈现

Rendered products/destroy.js.erb within layouts/standard the javascript fie is rendered

所以,现在我想这是使用JavaScript code的一个问题:

So now I guess it's a problem with the javascript code:

$(文件)。就绪(函数(){     $(#所属类别)HTML(&LT;%= escape_javascript(渲染(:部分=>列表))%>); });

$(document).ready(function() { $("#product_list").html("<%= escape_javascript( render(:partial => "list") ) %>"); });

时的那种code。通过原型的支持?我不使用jQuery ...

Is that kind of code supported by prototype? I don't use jQuery...

推荐答案

找到我自己的答案是:

/app/controllers/products.rb

  def destroy
    Product.find(params[:id]).destroy
    @products = Product.all
    respond_to do |format|
        format.js { render :layout=>false }
    end
  end

/app/views/products/destroy.js.erb

$("product_list").update("<%= escape_javascript(render(:partial => "list")) %>");

为什么没有教程,code例子或任何明确的rails3阿贾克斯啄? 我不得不使用code部位从5个不同的博客,论坛和管型,混合他们在一起,直到我找到正确的组合...叹息

Why is there NO tutorial, code example or anything clear about the ajax thingy in rails3? I had to use code parts from 5 different blogs, forums and casts, and mix them all together until I find the right combination... sigh