24

In an ActiveAdmin page, I would like to include a link to a list of related resources. For example, given that a

  • Site has_many Sections and,
  • Section belongs_to a Site (in my ActiveRecord models),

I would like my Site's show page to include a link to Sections within the site, which would go to the Section index page, with the Site filter preset.

Note that

  • I do not want to use ActiveAdmin's belongs_to function;
  • I don't want nested resources for a number of reasons (depth of nesting > 2, as well as usability concerns).

What I want is to generate a URL similar to the one ActiveAdmin generates if I first go to the Sections index page and then filter by Site.

The query parameter list generated by ActiveAdmin's filtering feature is pretty crazy; is there a helper method I could use to achieve this goal?

Thanks!

Community
  • 1
  • 1
jgshurts
  • 709
  • 5
  • 7

4 Answers4

29

I use this syntax:

link_to "Section", admin_sections_path(q: { site_id_eq: site.id })
Dorian
  • 22,759
  • 8
  • 120
  • 116
21

I worked out a reasonably satisfactory solution after poking around in meta_search for a bit. Syntax is a bit clunky, but it does the trick.

index do
  ...
  column "Sections" do |site|
  link_to "Sections (#{site.sections.count})", :controller => "sections", :action => "index", 'q[site_id_eq]' => "#{site.id}".html_safe
  end
end
jgshurts
  • 709
  • 5
  • 7
  • 1
    As an additional note this can work with HABTM relationships too. The `'q[site_id_eq]'` section needs to be pluralized so something like this: `'q[sites_id_eq]'` – ScottJShea Jul 12 '13 at 21:09
3

As jgshurts pointed out, the trick is identifying that q[site_id_eq] query parameter.

However, if you don't like the clunky syntax, you can also just use a path helper:

link_to "Sections (#{site.sections.count})", admin_sections_path('q[site_id_eq]' => site.id)

The UrlHelper#link_to documentation shows additional examples of this.

Sam Blake
  • 657
  • 6
  • 13
2

#auto_link(resource, content = display_name(resource)) ⇒ Object

Automatically links objects to their resource controllers. If the resource has not been registered, a string representation of the object is returned.

The default content in the link is returned from ActiveAdmin::ViewHelpers::DisplayHelper#display_name

You can pass in the content to display

eg: auto_link(@post, "My Link")

ActiveAdmin.register Girl do
  index do
    selectable_column

    column :name do |girl|
      auto_link(girl, girl.name)
    end

    column :email
    column :created_at
    actions
  end

Useful-link: http://www.rubydoc.info/github/gregbell/active_admin/ActiveAdmin/ViewHelpers/AutoLinkHelper

Note: This is tested with ActiveAdmin (v1.1.0 and 2.0.0.alpha)
Hope this works with other version as well. Please update this answer if you are sure it works with other versions you know.

Shiva
  • 11,485
  • 2
  • 67
  • 84
  • That doesn't answer the question - the asker wanted to know how to prefill the filters on a resource's list page, not how to link to a specific resource. – javawizard May 05 '20 at 10:32