【问题标题】:Strong_parameters issue with embedded documents in MongoDB using Rails 4 and Mongoid 4.0.0使用 Rails 4 和 Mongoid 4.0.0 在 MongoDB 中嵌入文档的 Strong_parameters 问题
【发布时间】:2013-07-11 09:22:43
【问题描述】:

我只是想用 Rails 建立一个博客,使用 MongoDB 作为我的持久层。作为其中的一部分,我想在我的帖子中嵌入 cmets,但每次我这样做时,它都会调用 ActiveModel::ForbiddenAttributesError 失败,我知道这与 Rails 中的 strong_parameters gem 有关。这就是我的控制器的样子

class CommentsController < ApplicationController

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

  private
    def comment_params
    params.require(:comment).permit(:by, :published_on, :body)
  end
end

谁能看出我哪里出错了?

【问题讨论】:

    标签: mongoid ruby-on-rails-4 strong-parameters


    【解决方案1】:

    你不能像你一样只传递参数哈希。您必须改用permit 方法的返回值。像这样:

    @comment = @post.comments.create!(comment_params)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 2013-03-25
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多