I have two models, links and tags, associated through a third, link_tags. The following code is in my Link model.
Associations:
class Link < Acti
Take a look at the line of your code
<% f.fields_for :tags_attributes do |tag_form| %>
You need to use just :tags
instead of :tags_attributes
.
This will solve your issue
Make sure that You have build links and tags in your controller like
def new
@link = @current_user.links.build
@link.tags.build
end
In order for this to work, you need to pass in the right params hash:
params = {
:link => {
:tags_attributes => [
{:tag_one_attr => ...}, {:tag_two_attr => ...}
],
:link_attr => ...
}
}
And your controller will look like:
def create
@link = Link.create(params[:link]) # this will automatically build the rest for your
end
I found this here on stackoverflow:
Rails nested form with has_many :through, how to edit attributes of join model?
please tell me if it worked.
In your controller in the new action (that loads the form partial), are you building a @tag through your link?
So you should see something along the lines of:
@link = Link.new
@tag = @link.tags.build
It might be best to post the contents of the new and create action of your links_controller.
Try this:
<% f.fields_for :tags_attributes do |tag_form| %>
try
<% f.fields_for :tags do |tag_form| %>
(ie lose the _attributes in :tag_attributes) That's how I've usually done nested forms