I know that I shouldn’t be building a new associated image for my gallery in the Gallery view partial. However, I can’t figure out to do this in ActiveAdmin’s generated controller, which can apparently be customized using the “Controller” method in the resource file. How can I do this using the Controller method instead of the view partial?
Here is my Gallery resource file:
ActiveAdmin.register Gallery do
controller.authorize_resource
scope_to :current_admin_user
before_filter :block_access
controller do
def block_access
if params && params['q'] && params['q']['admin_user_id_eq']
params['q']['admin_user_id_eq'] = nil
end
end
end
form :partial => "form"
end
Here is my Gallery view partial:
<% new_image = @gallery.images.build %>
<%= semantic_form_for [:admin, @gallery] do |g| %>
<%= g.inputs "Details" do %>
<%= g.input :title %>
<%= g.input :images, :as => :check_boxes, :label_method => Proc.new { |image| image_tag(image.thumb_path, :alt => "") + content_tag("h3", image.title) } %>
<% end %>
<%= g.inputs :for => [:images, new_image], :name => "New Image" do |image| %>
<% if image.object.new_record? %>
<%= image.input :title %>
<%= image.input :asset, :as => :file %>
<% end %>
<% end %>
<%= g.buttons %>
<% end %>
UPDATE #1: Adding the following methods inside the “controller do” block (based on the suggestion of Thomas Watson) worked for creating/editing Galleries.
def new
@gallery = Gallery.new
@new_image = @gallery.images.build
new!
end
def edit
@gallery = Gallery.find(params[:id])
@new_image = @gallery.images.build
edit!
end
def update
@gallery = Gallery.find(params[:id])
@new_image = @gallery.images.build
update!
end
Active Admin depends on Inherited Resources for its action magic. You can open up any controller action and add stuff to it. You can even create your own instance variable and Active Admin will automatically use that instead of creating its own.
In your case you would do something like this if you where to build it on the new action: