R
R
Robert2015-06-19 11:46:11
ruby
Robert, 2015-06-19 11:46:11

Correct form organization for nested resources?

There is a :posts resource and there is a :tags resource nested within it.
When I create a post, I want to immediately add tags to it, doing it with a form

<%= form_for [@post, @tag] do |form| %>
....

These two models are linked by a has_and_belongs_to_many association, so in the controller I naturally initialized two objects:
@post = Post.new
@tag = @post.tags.build
...

But the problem is that in my case @postit has id == nil, i.e. post has not been created yet.
How in this situation to make tags at once at creation of a post?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jeiwan, 2015-06-19
@Jeiwan

change has_and_belongs_to_many to has_many through and use accepts_nested_attributes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question