In an ActiveAdmin page, I would like to include a link to a list of related resources. For example, given that a
has_many Sections and,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.
belongs_to function;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!
I use this syntax:
link_to "Section", admin_sections_path(q: { site_id_eq: site.id })
                        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
                        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.
#auto_link(resource, content = display_name(resource)) ⇒ ObjectAutomatically 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_nameYou 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With