ActiveModel::ForbiddenAttributesError - strong par

2020-05-04 05:05发布

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

With this code:

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

Obviously it has something with strong param

1条回答
三岁会撩人
2楼-- · 2020-05-04 05:28

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

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

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

Read more about strong parameters in the Rails documentation.

查看更多
登录 后发表回答