Rails 4 Unpermited Parameters嵌套属性curl测试



使用导轨4.0.2我在使用curl创建一个针对新博客文章的嵌套评论数组时遇到了问题。我可以使用curl创建一个新的博客文章,但我在日志中得到了"未经许可的参数:评论",当我尝试创建链接到新文章的评论时,没有创建任何评论。

我在Post模型中添加了accepts_nested_attributes_for,并更新了Post控制器中的Post_params方法以接受comments_attributes。

我想在应用程序上运行curl,并在同一个电话中创建一个新的帖子和评论。我的curl调用有问题吗?或者我在嵌套属性设置中遗漏了什么?如有任何建议,不胜感激。

这创建了一个新的帖子,但没有评论:

curl -i -X POST -d 'post[title]=curlTest6 &post[comments][comment][commenter]=TestComment' http://localhost:3000/posts

上述呼叫的日志:

Processing by PostsController#create as */*
  Parameters: {"post"=>{"title"=>"curlTest6 ", "comments"=>{"comment"=>{"commenter"=>"TestComment"}}}}
Unpermitted parameters: comments
   (0.1ms)  begin transaction
  SQL (1.2ms)  INSERT INTO "posts" ("created_at", "title", "updated_at") VALUES (?, ?, ?)  [["created_at", Mon, 09 Dec 2013 15:27:18 UTC +00:00], ["title", "curlTest6 "], ["updated_at", Mon, 09 Dec 2013 15:27:18 UTC +00:00]]
   (1.0ms)  commit transaction

注释模型

class Comment < ActiveRecord::Base
  belongs_to :post
end

后期模型

class Post < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  accepts_nested_attributes_for :comments, :allow_destroy => true

  validates :title, presence: true,
                      length: { minimum: 5 }
end

张贴控制器

class PostsController < ApplicationController
  skip_before_filter :verify_authenticity_token, :only => [:create]
  def new
  end
  def create
    @post = Post.new(post_params)
    if @post.save
        redirect_to @post
      else
        render 'new'
    end
  end

  def show
    @post = Post.find(params[:id])
  end
  def edit
    @post = Post.find(params[:id])
  end
  def update
    @post = Post.find(params[:id])
    if @post.update(params[:post].permit(:title, :text))
      redirect_to @post
    else
      render 'edit'
    end
  end
  def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to posts_path
  end

  def index
    @posts = Post.all
  end
  private
  def post_params
     params.require(:post).permit(:title, :text, comments_attributes:[:commenter, :body])
   end
end

注释控制器

class CommentsController < ApplicationController
  def create
      @post = Post.find(params[:post_id])
      @comment = @post.comments.create(params[:comment].permit(:commenter, :body))
      redirect_to post_path(@post)
    end
    def destroy
       @post = Post.find(params[:post_id])
       @comment = @post.comments.find(params[:id])
       @comment.destroy
       redirect_to post_path(@post)
     end 
end

首先,这里不使用Comments控制器,只使用Posts控制器的create方法。控制器用于操作,而不一定是资源。

对于这些参数:

{"post"=>{"title"=>"curlTest6 ", "comments"=>{"comment"=>{"commenter"=>"TestComment"}}}}

你会用来强化他们

params.require(:post).permit(:title, {comments: [ { comment: [ :commenter ] } ]}) 

还要注意,对于accepts_nested_attributes_for,通常您希望参数是:comments_attributes而不是comments,然后没有其他命名空间(因此没有comment。请在此处阅读更多信息。

params.require(:post).permit(:title, {comments_attributes: [ :commenter ]})

卷曲的请求是:

$ curl -i -X POST -d 'post[title]=curlTest6 &post[comments_attributes][][commenter]=TestComment' http://localhost:3000/posts

这是因为你需要能够更新帖子以添加更多评论吗?也许控制器的update方法中的这一行就是问题所在:

if @post.update(params[:post].permit(:title, :text))

把它改成这样合理吗:

if @post.update(post_params)