ActiveModel :: ForbiddenAttributesError - 强参数

时间:2014-12-13 23:10:54

标签: ruby-on-rails ruby-on-rails-4

所以我正在关注一个明显在rails 3中完成的教程,我正在使用rails 4.我收到此错误:

  

ActiveModel :: ForbiddenAttributesError

使用此代码:

def create
    @movie = Movie.create!(params[:movie])
    flash[:notice] = "#{@movie.title} was successfully created."
    redirect_to movies_path
end

显然它有强大的参数

1 个答案:

答案 0 :(得分:2)

您需要确保创建Movie所需的所有属性都列入白名单。

在控制器中定义这样的方法:

private
def movie_params
  params.require(:movie).permit(:title, :rating, :release_date)
end

然后将方法的结果传递给create!

def create
  @movie = Movie.create!(movie_params)
  # ...
end

Read more about strong parameters in the Rails documentation