Rails - 选择多个页面元素并对其执行操作 [英] Rails - select multiple page elements and perform action on them

查看:32
本文介绍了Rails - 选择多个页面元素并对其执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Rails 中开发简单的照片库应用程序.我有一个显示缩略图的视图,每个都有单独的按钮(删除或移动该照片到不同的相册.

我想要的是可以选择其中的一堆并对所有选定的人使用操作.

勾选框就足够了,但最好是我可以通过 ctrl-click、shift-click 或用鼠标创建方格来选择它们.另一种方法是先按操作按钮,然后单击缩略图并确认.

我或多或少知道如何从头开始制作这些东西(使用 HTML、JS、jquery 和 PHP),但没有必要再发明轮子了,花很多时间去做,然后让它变得更糟一个已经可用..

是否有宝石可以实现这一目标?或者至少有一些有用的辅助函数(例如 form_for)?

解决方案

我们以前做过这个.这是我们所做的(我们使用 inherited_resources):

#config/routes.rb资源:控制器做删除 'destroy(/:id)', action: 'destroy', as: 'multi_destroy'结尾#app/controllers/your_controller.rb销毁id = 参数[:id]#多如果 id.kind_of?(Array)ids = Model.where(:id => id.split(','))ids.destroy_all 如果 ids.exists?response_to do |格式|format.json { 渲染 json:成功".to_json }format.html { redirect_to collection_path }结尾别的破坏!{ collection_path }结尾结尾#app/views/images/index.html.erb<div class="图片"><% collection.each do |image|%><!-- 图片--><div class="image" id="<%= image.id %>"><%= check_box_tag "id[]", image.id, false, class: "multi-delete" %>

<%结束%><%= button_to "删除所选", multi_destroy_images_path, method: :delete, id: "multi-delete", data: { confirm: "Are You Sure?"}%>

I'm working on simple photo gallery app in Rails. I have a view where thumbnails are displayed and I have separate buttons for every (to delete or move that photo to different album.

What I would like to have is a possibility to select a bunch of them and use action for all selected.

Tick-boxes would be good enough but best would be if I can select them with ctrl-click, shift-click or creating squere with mouse. One other way would be press action button first, then click thumbnails and confirm.

I know more or less how to make such of things from scratch (using HTML, JS, jquery and PHP) but there's no point to invent the wheel one more time, spend a lot of time doing it and make it worst that the one is already available..

Is there a gem to achieve this ? Or at least some useful helper functions (like form_for for example) ?

解决方案

We've done this before. Here's what we did (we use inherited_resources):

#config/routes.rb
resources :controller do
   delete 'destroy(/:id)', action: 'destroy', as: 'multi_destroy'
end

#app/controllers/your_controller.rb
def destroy
    id = params[:id] 

    #Multi
    if id.kind_of?(Array)
        ids = Model.where(:id => id.split(','))
        ids.destroy_all if ids.exists?

        respond_to do |format|
            format.json { render json: "success".to_json }
            format.html { redirect_to collection_path }
        end

    else
        destroy! { collection_path }
    end

end


#app/views/images/index.html.erb
<div class="images">
    <% collection.each do |image| %>

            <!-- Image -->
            <div class="image" id="<%= image.id %>">
                <%= check_box_tag "id[]", image.id, false, class: "multi-delete" %>
            </div>

    <% end %>

    <%= button_to "Remove Selected", multi_destroy_images_path, method: :delete, id: "multi-delete", data: { confirm: "Are You Sure?" } %>
</div>

这篇关于Rails - 选择多个页面元素并对其执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆