【问题标题】:ActiveAdmin custom views which retain the ActiveAdmin layout保留 ActiveAdmin 布局的 ActiveAdmin 自定义视图
【发布时间】:2013-09-24 09:02:07
【问题描述】:

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

我成功地使用此代码在自定义视图中进行了自定义控制器渲染:

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

例如我的 index.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>

有没有办法保持 ActiveAdmin 布局?

【问题讨论】:

    标签: ruby-on-rails ruby activeadmin


    【解决方案1】:

    问题解决了。只需将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
    

    【讨论】:

    • @MFrazier 你能解释或链接到原因吗?
    • 不适合我。得到一个错误:undefined local variable or method 'view_factory' for :Arbre::Context
    • 为我的 erb 文件工作。
    【解决方案2】:

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

      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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      相关资源
      最近更新 更多