rails_admin 自定义操作的强制格式

rails_admin force format for custom action

如何在自定义操作中强制使用格式? 例如我有两种格式,默认情况下 rails_admin 使用 html 但我想使用 pdf 代替:

 register_instance_option :controller do
      proc do
        respond_to do |format|
          format.json { render json: @object }
          format.html { render @action.template_name }
          format.pdf {
            render :pdf => "file", :layout => 'rails_admin/pdf.html.haml'
          }
        end
      end
    end

好的,我试过了,效果很好: 将此添加到您的自定义操作中:

register_instance_option :default_format do
  'pdf' #your favorite format
end

并通过将此添加到 menu_for

来覆盖 rails_admin\application_helper
  format: action.try(:default_format)

完整menu_for方法

def menu_for(parent, abstract_model = nil, object = nil, only_icon = false ) # perf matters here (no action view trickery)
      actions = actions(parent, abstract_model, object).select { |a| a.http_methods.include?(:get) }
      actions.collect do |action|
        wording = wording_for(:menu, action)
        %(
          <li title="#{wording if only_icon}" rel="#{'tooltip' if only_icon}" class="icon #{action.key}_#{parent}_link #{'active' if current_action?(action)}">
            <a class="#{action.pjax? ? 'pjax' : ''}" href="#{url_for(action: action.action_name, controller: 'rails_admin/main', format: action.try(:default_format) ,model_name: abstract_model.try(:to_param), id: (object.try(:persisted?) && object.try(:id) || nil))}">
              <i class="#{action.link_icon}"></i>
              <span#{only_icon ? " style='display:none'" : ''}>#{wording}</span>
            </a>
          </li>
        )
      end.join.html_safe
end