::加载ActiveModel ForbiddenAttributesError.Devise

时间:2015-01-04 15:04:06

标签: ruby-on-rails devise

我尝试使用gem' devise'。我希望用户创建一个帖子。

posts_controller

  def create
    @post = current_user.posts.build(params[:post])
  end

但浏览器显示错误

ActiveModel::ForbiddenAttributesError

我需要创建一个Users控制器并将其写入强参数或什么?

如何解决?

抱歉我的英文不好

UPD

class PostsController < ApplicationController

  def index
    @posts = Post.all
  end

  def new
    @post = Post.new
  end

  def create
    @post = current_user.posts.build(params[:post])
  end

  private

    def post_params
      params.require(:post).permit(:title, :body, :user_id)
    end

end

1 个答案:

答案 0 :(得分:0)

您需要明确指定允许传递给模型的属性。

def create
  @post = current_user.posts.build(post_params)
  # ...
end

def post_params
  # Require params[:post] and allow its title and text key
  params.require(:post).permit(:title, :text) 
end