Skip to content

Instantly share code, notes, and snippets.

@ChrisSki
Created August 22, 2013 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisSki/6309613 to your computer and use it in GitHub Desktop.
Save ChrisSki/6309613 to your computer and use it in GitHub Desktop.
town list within state
ActiveAdmin.register_page "Dashboard" do
#controller do
# @states = State.all
# @town = Town.all
#end
menu :priority => 1, :label => proc{ I18n.t("active_admin.dashboard") }
content :title => proc{ I18n.t("active_admin.dashboard") } do
#div :class => "blank_slate_container", :id => "dashboard_default_message" do
# span :class => "blank_slate" do
# span I18n.t("active_admin.dashboard_welcome.welcome")
# small I18n.t("active_admin.dashboard_welcome.call_to_action")
# end
#end
columns do
column do
panel "States" do
table_for State.limit(5) do
column :id
column :name do |state|
link_to state.name, admin_state_path(state)
end
end
end
end
column do
panel "Towns" do
ul do
State.all.each do |state|
li link_to(state.name, admin_state_path(state))
ul do
Town.all.each do |town|
li town.name
end
end
end
end
end
end
end
# Here is an example of a simple dashboard with columns and panels.
#
# columns do
# column do
# panel "Recent Posts" do
# ul do
# Post.recent(5).map do |post|
# li link_to(post.title, admin_post_path(post))
# end
# end
# end
# end
# column do
# panel "Info" do
# para "Welcome to ActiveAdmin."
# end
# end
# end
end # content
end
@ChrisSki
Copy link
Author

I'm a little confused on how to access the association of a model. I understand the code is producing a list of all towns for each state here, but how do I get just the towns of each state to be listed?

My State model has_many :towns, and the Town model belongs_to :state

Current output:
https://docs.google.com/file/d/0BwMz3RH42HtQdEVOUTI3VjFPT2c/edit?usp=drivesdk

@hassanshamim
Copy link

I've never really used Active Admin, but I think it should look something like

      column do
        panel "Towns" do
          ul do
            State.all.each do |state|
              li link_to(state.name, admin_state_path(state))
              ul do
                state.towns.each do |town|
                  li town.name
                end
              end
            end
          end
        end
      end
    end

If you have the relations set up properly, this should work. I'd jump into the rails console and do

state = State.first #pulls first state in your db
towns = state.towns #should return a list of town objects that belong to that state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment