本文介绍了Rails 3 - 使用复选框删除多条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 Rails 3 中的复选框删除多条记录?
How do you delete multiple records using checkboxes in Rails 3?
推荐答案
routes.rb:
resources :blog_posts do
collection do
delete 'destroy_multiple'
end
end
index.html.erb:
index.html.erb:
<%= form_tag destroy_multiple_blog_posts_path, method: :delete do %>
<table>
...
<td><%= check_box_tag "blog_posts[]", blog_post.id %></td>
...
</table>
<%= submit_tag "Delete selected" %>
<% end %>
blog_posts_controller.rb:
blog_posts_controller.rb:
def destroy_multiple
BlogPost.destroy(params[:blog_posts])
respond_to do |format|
format.html { redirect_to blog_posts_path }
format.json { head :no_content }
end
end
这篇关于Rails 3 - 使用复选框删除多条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!