保留 ActiveAdmin 布局的 ActiveAdmin 自定义视图



我有一个带有ActiveAdmin gem的rails 3应用程序。我的目标是在自定义视图中呈现一个自定义控制器,以保持布局。

我成功地使用以下代码在自定义视图中渲染自定义控制器:

页.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

例如我的索引.html.erb文件:

<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>

有没有办法保持活动管理员布局?

问题解决了。只需将layout 'active_admin'添加到您的代码中:

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

代码在这里:对于具有管理布局的无模型视图

  ActiveAdmin.register_page "myname" do
  content only: :index do
    render 'index'
  end
  controller do
     def index
     end
  end
end

然后在 app/views/admin/myname/ 下添加视图作为 _index.html.erb

最新更新