Last active
December 21, 2015 13:39
-
-
Save ChrisSki/6314060 to your computer and use it in GitHub Desktop.
Associations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
state.rb
works as expected, and outputs the towns that are associated. Intown.rb
, I'm trying to access the states that the town belongs to. I have the correcthas_many
andbelongs_to
associations set up in the models.