Jérémy Pouyet Jérémy Pouyet - 6 months ago 43
Ruby Question

ActiveAdmin custom views which retain the ActiveAdmin layout

I have a rails 3 app with the ActiveAdmin gem. My goal is to make a custom controller rendering in custom views which keep it layout.

I succeeded to make a custom controller rendering in custom views with this code :

pages.rb :

ActiveAdmin.register_page 'Pages' do

content only: :index do
render 'index'
end

content only: :edit do
render partial: 'edit'
end

controller do
def index
@search = Page.includes(:translations).where("page_translations.locale='fr'").metasearch(params[:search])
@pages = @search.page params[:page]
end

def edit
@page = Page.find params[:id]
end
end
end


And for example my index.html.erb file :

<h1>Pages</h1>
<%= form_for @search, :url => admin_pages_path, :html => {:method => :get, :class => "form-inline" } do |f| %>
<%= f.text_field :translations_name_contains , :class => "search-query input-medium focused" %>
<%= f.submit("Search", :class => "btn btn-primary") %>
<% end %>

<table>
<thead>
<tr>
<th><%= sort_link @search, :translations_name, "Titre" %></th>
<th><%= sort_link @search, :permalink, "Permalien" %></th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @pages.each do |page| %>
<tr>
<td><%= page.name %></td>
<td><%= page.permalink %></td>
<td><%= link_to("Update",edit_admin_page_path(page)) %></td>
</tr>
<% end %>
</tbody>
</table>


Is there a means to keep the ActiveAdmin layout ?

Answer

Problem solved. Just add layout 'active_admin' to your code :

controller do
  layout 'active_admin' # <-- here
  def index
    @search = Page.includes(:translations).where("page_translations.locale='fr'").metasearch(params[:search])
    @pages = @search.page params[:page]
  end

  def edit
    @page = Page.find params[:id]
  end
end
Comments