Lets say I have a PostsController and I have a Post model and Comment model. I'm allowing comments to be made on a Post, so on my posts/show I put the following
# routes.rb
resource :post do
member do
post :add_comment
end
end
# post.rb
has_many :comments
# comment.rb
belongs_to :post
<%= form_with(model: @post, url: add_comments_post_path) do |form| %>
<%= form.text_area :body %>
<% end %>
# posts_controller
def add_comment
post = Post.find(params[:id])
post.comments.create(params[:comment])
end
private
params.require(:post).permit(:title, :body)
I tried adding :comment
on permit but it raises ActiveModel::ForbiddenAttributesError
I got the Strong Parameters ActiveModel::ForbiddenAttributesError fixed like so:
params.require(:post).permit(:title, :body, comment_attributes: [:body])