ActiveAdmin how to add a custom controller without model [duplicate]

独自空忆成欢 提交于 2020-01-03 14:48:33

问题


Possible Duplicate:
Add page to active admin

I currently looking to a solution for adding a controller without a model to the admin generate by ActiveAdmin (and Rails 3.1). Of course I'd like to add a new menu in the navbar.

Using ActiveAdmin.register MyControllerWithoutModel do isn't working.

Edit : This question is a duplicate of Add page to active admin but no answer found.


回答1:


This is what worked for me, just substitute the right name for ViewLogger in the codeblocks. This way you wont have to create a dummy table in your database.

Make a file /app/models/viewlogger.rb with this contents, for more advanced tableless models you might want to check out http://keithmcdonnell.net/activerecord_tableless_model_gem.html or google your own insight together.

class Viewlogger < ActiveRecord::Base

  def self.columns 
    @columns ||= []
  end

  # ...  

end

add an entry to /config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable %w( viewlogger )
end

set up a route for your viewlogger, in config/routes.rb:

match '/admin/viewlogger' => 'admin/viewlogger#index', :as => :admin_viewlogger

now you can formulate the activeadmin register block as follows (make you sure you create a view partial in the right place)

ActiveAdmin.register Viewlogger do
  config.comments = false
  before_filter do @skip_sidebar = true end
  # menu false
  config.clear_action_items!   # this will prevent the 'new button' showing up


  controller do
    def index
      # some hopefully useful code
      render 'admin/viewlogger/index', :layout => 'active_admin'
    end
  end   

end




回答2:


I was looking for this to edit application configuration, but it seams that without model it is impossible. I just created empty model with table in db and register the resource as usual.

To customize resource:

disable filters

config.clear_sidebar_sections!

custom menu path

ActiveAdmin.application.namespaces[:admin].resources['Configuration'].namespace.menu.items.each{|i| i.instance_eval('@cached_url[:admin_configurations_path] = "/admin"')}


来源:https://stackoverflow.com/questions/7706122/activeadmin-how-to-add-a-custom-controller-without-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!