开发者

Rails 3 - remote delete

开发者 https://www.devze.com 2023-03-30 21:31 出处:网络
In an ERB Rails view <%= link_to(\"Destroy\", foos_path(1), :method => :delete, :confirm => \"Are you sure?\") %>

In an ERB Rails view

<%= link_to("Destroy", foos_path(1), :method => :delete, :confirm => "Are you sure?") %>

With a link_to lik开发者_运维知识库e the above you can process the delete for a foo with an ID of 1. How do make it remote and (a) still control being redirected to a page of my control or (b) call custom JavaScript (the goal of which would be jQuery to refresh a list)


Add :remote => true to the link_to will create a remote link.

In your controller method at the end put

respond_to do |format|
  format.js { render "my_method"}
end

You can omit the name of the ajax file if it's the same name as the method.

In my_method.js.erb simply call javascript functions, and you can embed erb ie:

$("#someDiv").load(<%= @some_value $>);

You can redirect to another page using standard javascript/jquery if you choose.

0

精彩评论

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