public
Last active

town list within state

  • Download Gist
dashboard.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
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

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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.