自定义activeadmin的布局(组件)

时间:2013-06-07 05:08:01

标签: ruby-on-rails activeadmin

Activeadmin布局不仅仅是一个文件,它还是一个组件集合。

如何使用activeadmin覆盖某些组件,例如logo,navigation。

2 个答案:

答案 0 :(得分:11)

activeadmin布局组件非常易于自定义。您需要做什么:只需定义一个打开ActiveAdmin :: View的模块。

您可以在初始化程序或管理目录中拥有custom_activeadmin_components.rb,您可以在其中定义所有activeadmin资源。我更喜欢将它放在activeadmin资源所在的目录中。然后覆盖您想要的任何模块:

这是一个示例:

module ActiveAdmin
  module Views
    class Header < Component
      def build(namespace, menu)
        super(:id => "header")

        @namespace = namespace
        @menu = menu
        @utility_menu = @namespace.fetch_menu(:utility_navigation)

        build_site_title
        build_global_navigation
        build_utility_navigation
        #you can add any other component here in header section 
      end

      def build_site_title
        render "admin/parts/logo"
      end

      def build_global_navigation
        render "admin/parts/main_nav"
      end

      def build_utility_navigation
        render 'admin/parts/language_options'
        insert_tag view_factory.global_navigation, @utility_menu, :id => "utility_nav", :class => 'header-item tabs'
        render 'admin/parts/branch_in_header'
      end
    end

    module Pages
      class Base
        def build_page_content
          build_flash_messages

          div :id => :wizard_progress_bar do
            render 'admin/parts/wizard_progress_bar'
          end

          div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
            build_main_content_wrapper
            build_sidebar unless skip_sidebar?
          end
        end
      end
    end  
  end
end

答案 1 :(得分:0)

您可以在admin / your_model.rb文件中自定义活动管理页面。

主动管理员的示例代码如下。

ActiveAdmin.register User do

  menu :label => "Our User", :priority => 3 #rename menu & set priority#
  #for index page#
  index :title => 'Our User' do #set page title#
    # index :download_links => false do
      selectable_column
      column :title
      column :category do |e| #want to change name of category
         e.categoryone
      end
      column :address 
      default_actions#this will add default action i.e. show|edit|delete#   
  end
  #end index#

  #for controller#
  controller do
    actions :all, :except => [:edit, :new] # you can decide which all methods to be shown in show page.
  end
  #end controller#

  #show start page#
  show do |user|
    h3 "User Details"
    attributes_table do
      row :title
      row :description      
      row :address, :label=>"User Address" #overide address label
      row :country
      row :approval
    end

    h3 "Activity Photoes"
    attributes_table do
      user.uploads.each do |img| #call associated model. Here i want to display uploaded image of upload.rb file
        row(" ") do 
          link_to image_tag(img.to_jq_upload['small_url']), admin_upload_path(img)#here i have added upload module to admin thats y i can directly give path to the upload record of admin module#
        end
      end
    end
    active_admin_comments # to show comment block of active admin
  end
  #show end#

  #filer(search) start righthand side panel#
  filter :title
  filter :category
  filter :address 
  #end filter#

  #for menu on show page#
  action_item only:[:show] do # this add "Approve User" button on the right hand side of show menu only as we have specified only show method
    if User.find(params[:id]).approval == true
      link_to "Approve User", approv_user_path(:id => params[:id])#call custom method of normal rails controller#
    end
  end
  #end menu#

  #start add side bar on page#
  sidebar "Project Details" do
    ul do
      li link_to("Profile", new_project_path)
    end
  end
  #end side bar#