rails_admin强制自定义操作的格式

时间:2015-02-04 18:16:07

标签: ruby-on-rails rails-admin

如何在自定义操作中强制格式化? 例如,我有2种格式,默认情况下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

1 个答案:

答案 0 :(得分:0)

好的,我试过这个并且工作正常: 将此添加到您的自定义操作:

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