Reputation: 245
I have 2 models with a HABTM relationship: authors and publications.
Using simple_form, I was able to create a add new publication
form that shows all of the possible authors as checkboxes. However, these associations are not saving to my db.
I know this is a params permit issue, but I'm not sure how to properly write it. Currently I have:
def publication_params
params.require(:publication).permit(:publication_id, :name, :year_published, :month_published, :day_published, :citation, :source, :abstract)
end
How do I write this so that rails can allow the association from the authors table (which has only two columns-- author_id
and name
) to be saved too?
Upvotes: 1
Views: 205
Reputation: 245
Got it!
I had to add accepts_nested_attributes_for :authors, :journals
into my publication model. Then in my publications_controller, I had to change my params to:
def publication_params
params.require(:publication).permit(:publication_id, :name, :year_published, :month_published, :day_published, :citation, :source, :abstract, :author_ids => [], :journal_ids => [])
end
This allowed me to save new associations to my db, and it updated my join table correctly.
Upvotes: 1
Reputation: 6870
Add author_id
to the params:
def publication_params
params.require(:publication).permit(:author_id, :publication_id, :name, :year_published, :month_published, :day_published, :citation, :source, :abstract)
end
Upvotes: 0