Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin - generate link to index, with filter preset?

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!

like image 845
jgshurts Avatar asked Apr 02 '12 22:04

jgshurts


4 Answers

I use this syntax:

link_to "Section", admin_sections_path(q: { site_id_eq: site.id })
like image 56
Dorian Avatar answered Oct 31 '22 16:10

Dorian


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
like image 21
jgshurts Avatar answered Oct 31 '22 16:10

jgshurts


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.

like image 3
Sam Blake Avatar answered Oct 31 '22 15:10

Sam Blake


#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.

like image 2
illusionist Avatar answered Oct 31 '22 15:10

illusionist