在我的应用程序上创建注释时,我得到了下面的。
错误消息,确切地说问题来自我的Comments Controller文件中的第7行:@comment = @pin.comments.create(params[:comment])
app/控制器/注释_Controller.rb
class CommentsController < ApplicationController
before_filter :authenticate_user!
def create
@pin = Pin.find(params[:pin_id])
@comment = @pin.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
private
def comment_params
params.require(:comment).permit(:body, :pin_id)
end
end以下是评论模型
class Comment < ActiveRecord::Base
belongs_to :pin
end对此错误消息有帮助吗?
发布于 2014-07-22 09:15:08
您应该将这一行替换为
@comment = @pin.comments.create(comment_params)此外,将pin_id放入允许的参数中是不必要的(因为您通过@pin.comments关联创建注释),而且可能不安全(用户可以将注释与其他Pin相关联)。
https://stackoverflow.com/questions/24883321
复制相似问题