-1

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

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Berimbolo
  • 293
  • 2
  • 12
  • Check the server log, you'll see the parameters of the request, you have to replicate that. What to require/permite depends on the actual form inputs, it's hard to tell since you are not showing the inputs. If you are not sure, do one of those form submissions and copy here log of that request. – arieljuod Oct 25 '20 at 03:06
  • is params {comment => {:body => "message}, id => 1} – Berimbolo Oct 25 '20 at 03:14
  • @Berimbolo can u post error log ? – 7urkm3n Oct 25 '20 at 03:42
  • ActiveModel::ForbiddenAttributesError – Berimbolo Oct 25 '20 at 03:50

2 Answers2

1

I got the Strong Parameters ActiveModel::ForbiddenAttributesError fixed like so:

params.require(:post).permit(:title, :body, comment_attributes: [:body])
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Berimbolo
  • 293
  • 2
  • 12
0

According to your comment, you have to do:

post = Post.find(params[:id])
post.comments.create(comment_params)

and permit it like:

private
def comment_params
  params.require(:comment).permit(:title, :body)
end
arieljuod
  • 15,460
  • 2
  • 25
  • 36
  • I tried, adding that second strong param but im forbidden attributes error on log – Berimbolo Oct 25 '20 at 03:25
  • update the question, add the controller code and the request log – arieljuod Oct 25 '20 at 03:26
  • Also, there's a full rails official guide about what you are doing, I'd suggest you read this first https://guides.rubyonrails.org/getting_started.html (it's a blog sample app with posts and comments, all code is there with steps and explanations) – arieljuod Oct 25 '20 at 03:28
  • I see you copied the code of your controller, it's not what I put in this answer, the first part is the add_comment action and the second is the strong parameters part. I'm not sure what did you try. – arieljuod Oct 25 '20 at 03:31
  • I updated the controller the way you did and the form as well. Still same error, params[:comment] is still an error – Berimbolo Oct 25 '20 at 03:37
  • I strongly recommend you to read the rails guide link I pasted in a previous comment. You have all the explanations and steps by step on how to do almost exactly what you want to do. – arieljuod Oct 25 '20 at 04:09
  • Okay I got it, turns out i need to define comment_attributes [:body] insted of comment: [:body] – Berimbolo Oct 25 '20 at 04:09