Question

I'm just trying to set up a blog with Rails using MongoDB for my persistence layer. As part of that I'd like to embed comments in my posts but every time I do, it fails calling ActiveModel::ForbiddenAttributesError, which I know has to do with the strong_parameters gem in Rails. this is what my controller looks like

class CommentsController < ApplicationController

  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create!(params[:comment])
    redirect_to @post
  end

  private
    def comment_params
    params.require(:comment).permit(:by, :published_on, :body)
  end
end

Can anyone see where I'm going wrong?

Était-ce utile?

La solution

You can't just pass the params hash like you do. You must use the return value of the permit method instead. Like this:

@comment = @post.comments.create!(comment_params)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top