Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom page in ActiveAdmin gem

Ruby 2.0, Rails 4.0, PSQL 9.3

In the ActiveAdmin documentation there is the example:

ActiveAdmin.register_page "My Page" do
  content do
    para "Hello World"
  end
end

Where do I put this code? The documentation says:

In the above example, a new page will be created at /admin/my_page with the title “My Page” and the content of “Hello World”.

This implies that such a file would be created automatically somehow? Nevertheless, I created a file named import.rb under app/admin and the Import item in the menu does appear. However, I am not able to use HTML, as this file is .rb and not .erb. I suppose, in order to be able to use html, I need to create a partial and den render it within the content method. But when I look under app/views there is not admin folder (only layouts). Does this mean I need to create the folder admin under app/views? If yes, where should I put my partial - directly under app/views/admin or under a new folder app/views/admin/import?

I am sorry for the menu questions, but the documentation of ActiveAdmin is pretty modest. I would really appreciate if someone could provide a more details explanation of the steps needed for creating and adding content to a new page in ActiveAdmin.

like image 471
Alexander Popov Avatar asked Sep 28 '13 12:09

Alexander Popov


1 Answers

What the documentation meant was that if you create a new custom page app/admin/my_page.rb, this page will be available in the URL /admin/my_page (if you are using the default ActiveAdmin configuration).

Regarding rendering of an ERB or HAML partials for your my_page.rb, you can do it this way:

ActiveAdmin.register_page "My Page" do
  content do
    render :partial => 'about'
  end
end

This will look under the directory app/views/admin/my_page/. If the directories do not exist, create them. Also, you can still specify other directories by referencing the full template path (e.g. shared/sections/about) like you would for a non-ActiveAdmin controller.

like image 84
kristinalim Avatar answered Oct 17 '22 20:10

kristinalim