2

在 routes.rb 中:

  namespace :admin, :format => false do
    resources :activities
    resources :categories
    resources :charities
  end 

添加所有 map.resources 额外的 RESTful 操作 csv_export:

创建 initilizer/mapper.rb 文件并添加模块 ActionDispatch::Routing::Mapper::Resources

module ActionDispatch::Routing::Mapper::Resources
  # Add csv_export to all resources as default action
  def resources(*resources, &block)
    options = resources.extract_options!.dup

    if apply_common_behavior_for(:resources, resources, options, &block)
      return self
    end

    resource_scope(:resources, Resource.new(resources.pop, options)) do
      yield if block_given?

      concerns(options[:concerns]) if options[:concerns]
      collection do
        get :csv_export ,defaults: { format: 'csv' } if   @scope[:path].include?("/admin")
        get  :index if parent_resource.actions.include?(:index)
        post :create if parent_resource.actions.include?(:create)
      end

      new do
        get :new
      end if parent_resource.actions.include?(:new)

      set_member_mappings_for_resource
    end

    self
  end
end
4

0 回答 0