错误:CommentsController #create中的ActiveModel :: ForbiddenAttributesError

时间:2015-10-08 14:45:13

标签: ruby-on-rails ruby-on-rails-4 controller

我尝试为帖子创建评论,它给了我这个错误

ActiveModel::ForbiddenAttributesError in CommentsController#create
ActiveModel::ForbiddenAttributesError

Extracted source (around line #6):

这是我的comments_controller.rb

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]

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

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @pin, notice: 'Comment was successfully created.' }
        format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end
end

这是我的评论模型

class Comment < ActiveRecord::Base
  belongs_to :post
end

这是我的评论创建表迁移

class CreateComments < ActiveRecord::Migration
  drop_table :comments
  def change
    create_table :comments do |t|
      t.integer :post_id
      t.integer :user_id
      t.text :content

      t.timestamps null: false
    end
  end
end

我希望你能帮助我。

3 个答案:

答案 0 :(得分:0)

好像你没有注册强烈的评论参数。

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]

  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(comment_params)

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @pin, notice: 'Comment was successfully created.' }
        format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  private
  def comment_params
    params.require(:comment).permit(:post_id, :user_id, :content)
  end
end

答案 1 :(得分:0)

你是否在档案中设置了参数,看起来你没有发布整个事情,但如果没有,请尝试这样的事情。

def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.new(comment_params)

然后在文件底部向下创建一个私有方法

private   
  def comment_params
    params.require(:comment).permit(:post_id,:user_id,:content)
  end

答案 2 :(得分:0)

您的comments_controller应该看起来像这样:

def new
  @comment = Comment.new
end

def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.new(comment_params)

  respond_to do |format|
    if @comment.save
      format.html { redirect_to @pin, notice: 'Comment was successfully created.' }
      format.json { render json: @comment, status: :created, location: @comment }
    else
      format.html { render action: "new" }
      format.json { render json: @comment.errors, status: :unprocessable_entity }
    end
  end
end

private

def comment_params
  params.require(:comment).permit(:post_id, :user_id, :content)
end

而且,我强烈建议您阅读Rails Official documentation for Strong Parameters