【问题标题】:Trying to post comment to my post via curl and it doesn't work试图通过 curl 对我的帖子发表评论但它不起作用
【发布时间】:2015-02-09 04:06:45
【问题描述】:

我有路线

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

我的评论有“内容”字段。假设我想对我的帖子发表评论。

所以我正在尝试通过 curl 执行此命令发表评论 curl -X POST -H "Content-Type: application/json" -d '{"comment": "hello"}' 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 '{"comment":{"comment":"test"}}' http://localhost:3000/posts/8/comments

为我工作,但我有点困惑,为什么当我为我的 POST 做类似的 curl 时: 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 哈希中传递帖子就可以工作?

【问题讨论】:

  • 嗨 - 如果你有一个异常......这意味着你的应用程序将生成一个堆栈跟踪 - 这对于帮助找出究竟出了什么问题非常有用。您通常会在您的服务器窗口或日志文件中找到它——它大约有 20-40 行各种文件的列表,最顶部有一条错误消息。找到它,然后编辑您的问题并将其全部粘贴到那里,我们可以帮助您找出问题所在。

标签: ruby-on-rails api curl


【解决方案1】:

你在这里说的是“对于评论对象,允许评论属性”

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

所以 rails 期待这样的东西:

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

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

【讨论】:

  • 是的,我的 Comment 模型有一个 comment textarea 属性。数据是这样的吗?我是否必须通过 comment: 作为键,因为它是嵌套路由?因为当我创建我的帖子时,我会做类似的帖子 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)
  • "comment": {"comment": "hello"}} 对我也不起作用。解析请求参数时出错。内容:{评论:{评论:
  • curl -X POST -H "Content-Type: application/json" -d '{"comment":{"comment":"test"}}' localhost:3000/posts/8/comments 工作
  • 堆栈溢出 cmets 没有良好的代码格式 - 因此,如果您尝试这样做并遇到错误,您可以编辑您的问题并说出您尝试过的内容并再次向我们提供完整的错误堆栈跟踪吗?
  • 可能你对帖子没有问题,因为没有“帖子”属性(只是标题等),也许 rails 在评论对象和评论属性之间感到困惑 -所以这就是为什么你需要它来发表评论而不是帖子?
猜你喜欢
  • 2019-08-07
  • 2011-11-09
  • 2017-07-29
  • 1970-01-01
  • 2021-09-15
  • 2011-10-12
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多