试图通过卷曲发表评论到我的帖子,它不起作用

时间:2015-02-09 04:06:45

标签: ruby-on-rails api curl

我有路线

 POST   /posts/:post_id/comments(.:format)             comments#create

我的评论有"内容"领域。让我们说我想在我的帖子上发表评论。

所以我试图通过curl发表评论这个命令 curl -X POST -H" Content-Type:application / json" -d' {"评论":"你好"}' http://localhost:3000/posts/2/comments

但是我得到了一个动作控制器:异常捕获

不确定原因

控制器

# POST /comments                                                                                                                                            
  # POST /comments.json                                                                                                                                       
  def create                                                                                                                                                  
    @comment = @post.comments.new(comment_params)                                                                                                             

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


# Never trust parameters from the scary internet, only allow the white list through.                                                                      
    def comment_params                                                                                                                                        
      params.require(:comment).permit(:comment)                                                                                                               
    end     

编辑:

error in log file
Completed 500 Internal Server Error in 2ms

NoMethodError (undefined method `permit' for "hello":String):
  app/controllers/comments_controller.rb:89:in `comment_params'
  app/controllers/comments_controller.rb:33:in `create'

编辑: curl -X POST -H" Content-Type:application / json" -d' {"评论":{"评论":"测试"}}' http://localhost:3000/posts/8/comments

为我工作但我有点困惑为什么当我为我的POST做类似的卷曲时: curl -H "Content-Type: application/json" -d '{"title":"funny title","content":"cool stuff", "user_id":2}' localhost:3000/posts和我的帖子参数如下params.require(:post).permit(:title, :content, :user_id)

为什么在没有在curl哈希中传递帖子的情况下工作?

1 个答案:

答案 0 :(得分:0)

你在这里说的是#34;对于评论对象,允许评论属性"

params.require(:comment).permit(:comment)

所以rails期待像:

{"comment": {"comment": "hello"}}

如果你想快速修复它 - >您需要将上述内容用于您的数据。 否则,您需要弄清楚真实评论表单的数据真实情况,并更新您的要求/许可行以匹配