将导出选项添加到Rails Admin自定义页面

时间:2019-02-12 15:27:13

标签: ruby-on-rails rails-admin

我在Rails Admin中具有以下自定义页面:

require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'

module RailsAdminSapSaeQualifiedSearch
end

module RailsAdmin
  module Config
    module Actions
      class SapSaeQualifiedSearch < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)


        register_instance_option :collection? do
          true
        end

        register_instance_option :link_icon do
          'fa fa-search'
        end

        register_instance_option :visible? do
          authorized? && bindings[:abstract_model].model == Customer
        end

        register_instance_option :http_methods do
          [:get, :patch]
        end

        register_instance_option :controller do
          Proc.new do

            if request.get?
              @users = []
            elsif request.patch?
              @users = User.send(params[:qualification], params[:start_at], params[:stop_at], params[:zip])
            end

            render :action => @action.template_name


          end
        end
      end
    end
  end
end

...以及相关视图:

<br>

<%= form_tag sap_sae_qualified_search_path(@abstract_model), method: :patch, class: "form-inline" do %>
<div class="form-group">
    <%= label_tag :qualification, "Qualification Type:" %>
    <%= select_tag :qualification, options_for_select([['SAP', 'sap_qualified'], ['SAE', 'sae_qualified']]), class: "form-control" %>
</div>

<div class="form-group">
    <%= label_tag :start_at, "Between:" %>
    <%= text_field_tag :start_at, params[:start_at] ||= 3.years.ago.to_date, class: "form-control customedate" %>
</div>

<div class="form-group">
    <%= label_tag :stop_at, "and:" %>
    <%= text_field_tag :stop_at, params[:stop_at] ||= Date.today, class: "form-control customedate" %>
</div>

<div class="form-group">
    <%= label_tag :zip, "Zip Code:" %>
    <%= text_field_tag :zip, params[:zip] %>
</div>

<%= button_tag "Search", class: "btn btn-default"%>

<% end %>


 <script>

$( ".customedate" ).datepicker();
</script>

<% if @users.any? %>

<%= @users.length %> Users found

<table class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Qualified Courses</th>
            <th>Licences</th>
            <th></th>
        </tr>
    </thead>

    <% @users.each do |user| %>
        <tr>
            <td width="1%" nowrap><%= user.full_name %></td>
            <td width="1%" nowrap><%= user.email %></td>
            <td width="1%" nowrap><%= user.phone_1 %></td>
            <td style="white-space: normal;"><%= user.sap_courses_passed.where('class_registrations.exam_completed_at >= ? AND class_registrations.exam_completed_at <= ?', params[:start_at].to_date.beginning_of_day, params[:stop_at].to_date.beginning_of_day).collect{|c| "<small><b>#{c.exam_completed_at.strftime('%F')}</b> -- #{c.course_class.title}</small>"}.join('<br>').html_safe %></td>
            <td><% user.licenses.each do |l| %>
                        <%= l.profession %>, <%= l.number %>, <%= l.state %><br>
                    <% end %>
            </td>
            <td width="1%" nowrap><%= link_to "Details", {:action => :show, :controller => 'rails_admin/main', :model_name => "Customer", :id => user.id }, class: "btn btn-default", target: "_blank" %></td>
        </tr>
    <% end %>
</table>

<% end %>

对于上下文,视图如下所示: enter image description here

我想在此视图中添加一个Rails Admin Export按钮。我尝试过

register_instance_option :export do
  true
end

...没有运气。任何帮助或想法表示赞赏。

1 个答案:

答案 0 :(得分:0)

您必须在rails admin config中完成

config/initializers/rails_admin.rb

RailsAdmin.config do |config|
  [...]
  config.actions do
    [...]
    sap_sae_qualified_search do
      only %w{ Customer }
    end
  end
end

如果这不起作用,您也可以尝试在config块之前在同一文件上注册操作:

RailsAdmin::Config::Actions.register(
  :sap_sae_qualified_search,
  RailsAdmin::Config::Actions::SapSaeQualifiedSearch
)