Question #1: Yes, you need to add the :category_attributes
in the post_params
strong parameter method like this:
def post_params
params.require(:post).permit(:body, category_attributes: [:title])
end
Question #2: Yes, the parameters should be nested, this is a typo in your view because you are not applying the fields_for
(plural by the way) in the scope of the parent form builder, also you are not using the category_fields
form builder inside the fields_for
block!
The view should look like this:
<%= form_for @post do |form| %>
<%= form.label :body %>
<%= form.text_area :body %>
<%= form.fields_for :category do |category_fields| %>
<%= category_fields.label :title %>
<%= category_fields.text_field :title %>
<% end %>
<%= form.button "Add Post" %>
<% end %>
Question #3: The button parameter might be in the wrong place due to the mixed up form building in your view.
Question #4: You don't need to build the child model in your create action if you accept nested attributes
Question #5: Yes, the validation of the child model is also run and if the validation of the child fails, the parent will also have an error and is not saved to the database.