Skip to content

Instantly share code, notes, and snippets.

@ChrisSki
Last active December 21, 2015 13:39
Show Gist options
  • Save ChrisSki/6314060 to your computer and use it in GitHub Desktop.
Save ChrisSki/6314060 to your computer and use it in GitHub Desktop.
Associations
ActiveAdmin.register State do
index do
column :name
column "Associated Towns" do |state|
ul do
state.towns.each do |town|
li link_to(town.name, admin_town_path(town))
end
end
end
end
controller do
def permitted_params
params.permit state: :name
end
end
end
ActiveAdmin.register Town do
index do
column :name
column "State" do |town|
town.state.each do |state|
link_to(state.name, admin_state_path(state))
end
end
end
controller do
def permitted_params
params.permit town: [:name, :state_id]
end
end
end
@ChrisSki
Copy link
Author

state.rb works as expected, and outputs the towns that are associated. In town.rb, I'm trying to access the states that the town belongs to. I have the correct has_many and belongs_to associations set up in the models.

@genericsteele
Copy link

Because towns only have one state, you don't need the loop from lines 6-8 in town.rb

Should just be able to call town.state.name instead of town.state.each ...

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