重写Controller操作后,Rails ActiveAdmin布局丢失

时间:2011-10-09 15:56:09

标签: activeadmin

覆盖没有'new!'的新控制器不显示ActiveAdmin布局。但是当我添加'新!'时虽然我做了'@ resource.build_synchronization',但是没有出现嵌套的'同步'表单。不太确定我在这里做错了什么。

案例#1(ActiveAdmin布局已消失)

ActiveAdmin.register Resource do
  controller do
      # This code is evaluated within the controller class
      def new
        @resource = Resource.new
        @resource.build_synchronization
      end
  end
end

案例#2(不显示嵌套表单同步)

ActiveAdmin.register Resource do
  controller do
      # This code is evaluated within the controller class
      def new
        @resource = Resource.new
        @resource.build_synchronization
        new!

      end
  end
end

视图\管理员\资源\ new.html.erb

<%= semantic_form_for [:admin, @resource] do |form| %>
    <%= form.inputs "Resource", :id => "resource" do %>
        <%= form.input :name %>
        <%= form.semantic_fields_for :synchronization do |sync| %>
            <% sync.inputs  :name => "Synchronization", :id => "synchronization"  do %>
                <%= sync.input :start_datetime, :as => :datetime %>
                <%= sync.input :repeat_interval, :as => :radio, :collection => @intervals %>
                <%= sync.input :repeat_type, :as => :select, :collection => ["Manual", "Automatic"] %>
            <% end %>
        <% end %>
    <% end %>
    <%= form.buttons %>
<% end %>
<% end %>

模型:

class Resource < ActiveRecord::Base
  has_one :synchronization
  accepts_nested_attributes_for :synchronization
end


class Synchronization < ActiveRecord::Base
  belongs_to :resource
  has_many :mappings
  accepts_nested_attributes_for :mappings
  #validates_presence_of :start_datetime
end

2 个答案:

答案 0 :(得分:2)

对于CRUD操作,activeadmin不使用标准布局

lib/active_admin/resource_controller.rb

    # Determine which layout to use.
    #
    #   1.  If we're rendering a standard Active Admin action, we want layout(false)
    #       because these actions are subclasses of the Base page (which implementes
    #       all the required layout code)
    #   2.  If we're rendering a custom action, we'll use the active_admin layout so
    #       that users can render any template inside Active Admin.
    def determine_active_admin_layout
      ACTIVE_ADMIN_ACTIONS.include?(params[:action].to_sym) ? false : 'active_admin'
    end

您可以手动定义布局

  controller do
    layout 'active_admin', :only => [:new]
  end

答案 1 :(得分:1)

您需要将form.semantic_fields_for语句放在form.inputs块中。

另外,我不会将form.buttons放在form.semantic_fields_for块或form.inputs块中。它应该是semantic_form_for块下的直接子节点(这不是导致您出现问题的原因,而只是您通常放置此位置的位置)。