ActiveModel::ForbiddenAttributesError - 强参数 [英] ActiveModel::ForbiddenAttributesError - strong parameters

查看:29
本文介绍了ActiveModel::ForbiddenAttributesError - 强参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在学习一个显然是在 rails 3 中完成的教程,我正在使用 rails 4.我收到这个错误:

So I am following one tutorial that is obviously done in rails 3 and I am using rails 4. I get this error:

ActiveModel::ForbiddenAttributesError

ActiveModel::ForbiddenAttributesError

使用此代码:

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

显然它有一些强参数

推荐答案

您需要确保创建电影所需的所有属性都已列入白名单.

You need to make sure that all attributes required to create a Movie are whitelisted.

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

Define a method like this in your controller:

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

然后将方法的结果传入create!:

And then pass the result of the method into create!:

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

阅读 Rails 文档中有关强参数的更多信息.

这篇关于ActiveModel::ForbiddenAttributesError - 强参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆