我有一个简单的产品列表,其中包含编辑和删除链接作为图像.
删除产品时,列表不会更新.刷新页面表明产品确实已被删除.
/app/views/products/list.rhtml
<div id="product_list"> <%= render :partial => 'list' %> </div>
/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
def destroy Product.find(params[:id]).destroy @products = Product.all end
/app/views/products/destroy.rjs(不知道该怎么办……)
$(document).ready(function() { $("#product_list").html("<%= escape_javascript( render(:partial => "list") ) %>"); });
所以远程链接似乎工作正常.
我不知道如何使用ajax回调来更新#product_list
$(document).ready(function(){ $('#product_list').bind("ajax:success",function(evt,data,status,xhr){ alert('hello'); }) });
但它没有被执行(这可能不是原型的有效代码)而且我不知道要放入什么代码以便我的列表在销毁产品后得到更新
任何帮助(除了“使用jQuery”)非常感谢!
编辑:这是删除操作的服务器日志(在我将上面的javascript移动到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处理#untroy作为JS,因此远程链接工作
[36mProduct Load(0.0ms)[0m [1mSELECT产品. FROM products *执行@products = Product.all
在layouts / standard中渲染产品/ destroy.js.erb呈现javascript fie
所以现在我猜这是javascript代码的一个问题:
$(document).ready(function(){
$(“#product_list”).html(“<%= escape_javascript(render(:partial =>”list“))%>”);
});
/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")) %>");
为什么在rails3中没有关于ajax thingy的教程,代码示例或任何明确的内容?我不得不使用来自5个不同博客,论坛和演员的代码部分,并将它们混合在一起,直到我找到合适的组合……叹息