简体   繁体   English

Rails-选择多个页面元素并对它们执行操作

[英]Rails - select multiple page elements and perform action on them

I'm working on simple photo gallery app in Rails. 我正在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. 勾选框将足够好,但最好的方法是,我可以使用ctrl单击,shift单击或使用鼠标创建squere来选择它们。 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.. 我或多或少知道如何从头开始做这些事情(使用HTML,JS,jquery和PHP),但是没有必要再浪费时间,花很多时间去做,使事情变得更糟的是没有意义的。已经可用。

Is there a gem to achieve this ? 有没有实现这一目标的宝石? Or at least some useful helper functions (like form_for for example) ? 或至少一些有用的辅助函数(例如form_for )?

We've done this before. 我们之前已经做到了。 Here's what we did (we use inherited_resources ): 这是我们所做的(我们使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM