开发者

Rails ActiveAdmin Layout was lost after overriding Controller action

Overriding a new controller without 'new!' does not display ActiveAdmin layout. But then when I added 'new!' nested 'synchronization' form is not appearing although I did '@resource.build_synchronization'. Not so sure what I'm doing wrong here.

case #1 (ActiveAdmin layout is gone)

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

case #2 (nested form synchronization does not appear)

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

views\admin\resources\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.b开发者_如何学Gouttons %>
<% end %>
<% end %>

models:

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


For CRUD actions active admin don't use standart layout

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

You can define layout manually

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


You need to put the form.semantic_fields_for statement inside a form.inputs block.

Also, I would not put form.buttons inside neither the form.semantic_fields_for block nor a form.inputs block. It should be a direct child under the semantic_form_for block (this is not what is causing your problem, but just the location where you would normally put this).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜