2
votes

I added ActiveAdmin to my application and successfully changed the index-method for my resource. Now when I click on 'new resource' it brings me to the new-method just fine, however, there's a button missing (paperclip) in order to allow the user to upload an image-attachment.

I can not find a way to edit the view, neither do I find a way to completely rewrite the new-method.

If you need any code by me I can paste everything here.

Thanks! // Check the very bottom of this post for the solution!

// This is how I tried it, but it's not working. The changes I applied to 'app/admin/entry.rb' for my index-method worked, but the 'new'-method just doesn't work at all.

app/admin/entry.rb:

ActiveAdmin.register Entry do

  index do
    column :id
    column :description
    column :created_at
    column :image_content_type
    column do |entry|
      links = link_to "Edit", edit_admin_entry_path(entry)
      links += " "
      links += link_to "Delete", admin_entry_path(entry), :method => :delete, data: { confirm: "Are you sure?" }
      links
    end
  end

  def new
    form_for @entry, :html => {:multipart => true} do |f|
      f.label :description
      f.text_area :description
      f.file_field :image
    end
    f.submit 'Save'
  end

end

Before I added ActiveAdmin I just added a scaffold for Entry and used it like this: entries_controller.rb:

  def new
    @entry = Entry.new
  end

View (new.html.slim):

h1 New entry

== render 'form'

= link_to 'Back', entries_path

The rendered form (_form.html.slim):

= form_for @entry, :html => {:multipart => true} do |f|
  - if @entry.errors.any?
    #error_explanation
      h2 = "#{pluralize(@entry.errors.count, "error")} prohibited this entry from being saved:"
      ul
        - @entry.errors.full_messages.each do |message|
          li = message

  .field
    = f.label :description
    = f.text_area :description
    = f.file_field :image
  .actions = f.submit 'Save'

Now, while this still works when heading to localhost:3000/entries/new it simply shows the default view for localhost:3000/admin/entries/new

If you got any help, that'd be highly appreciated! Is there any way to see the existing code that ActiveAdmin already uses somehow? I may change it to my needs by simply adding that one field I need.

// SOLUTION:

app/admin/resource.rb

ActiveAdmin.register Entry do
  permit_params :image, :description

  index do
    column :id
    column :description
    column :created_at
    column :image_file_name
    column :image_content_type
    column do |entry|
      links = link_to "Edit", edit_admin_entry_path(entry)
      links += " "
      links += link_to "Delete", admin_entry_path(entry), :method => :delete, data: { confirm: "Are you sure?" }
      links
    end
  end

  form do |f|
    f.inputs "New Entry" do
      f.input :description
      f.input :image
    end
    f.actions
  end

    

end
1

1 Answers

3
votes

You can customize both the controller actions and the new resource view.

To edit the new action in the controller:

#app/admin/your_resource.rb

controller do
  def new
    @resource = Resource.new
    .... # Your custom logic goes here
  end
end

To edit the new resource view and add an image with paperclip.

#app/admin/your_resource.rb

form html: { multipart: true } do |f|
  f.inputs "Resource Details" do
    f.input :title
    .... # Your input fields

    # This adds the image field. Be careful though 
    # the field name needs to be the same in your model

    f.input :image, as: :file, required: false
  end

  f.actions
end