Rails 4.0.4 ActiveModel::ForbiddenAttributesError

Author posted @ 2014年3月23日 18:50 in Ruby , 3425 阅读

Problem:

When I try the rails example project ''rottenpotatoes'' on the SaaS book by Fox and Petterson,  I had problem running the "create" and "update" methods on the webpage. As figure shows, when I click button "Save Changes", it calls the Movie.update function and throws out an error "ActiveModel::ForbiddenAttributesError"

Explanation:

The example code on the text book are 1) http://pastebin.com/g5nq88eJ

# in movies_controller.rb
def create
  @movie = Movie.create!(params[:movie])
  redirect_to movies_path
end

This example works for Rails version < 4. Rails 4.0.4 uses strong parameters[1], so "attr_accessible" and "attr_protected" are out-dated.

Solution: 

def create
    @movie = Movie.new(movie_params)
    if @movie.save
      flash[:notice] = "#{@movie.title} was successfully created."
      redirect_to movies_path
    else
      render 'new' # note, 'new' template can access @movie's field values!
    end
end

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

see all code: https://github.com/ysonggit/saas-rottenpotatoes

References:

[1] http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

Harga Axioo 说:
2018年5月31日 01:46

it calls the Movie.update function and throws out an error "ActiveModel::ForbiddenAttributesError

Gclub 说:
2019年6月28日 00:40

Thank you for making me realize this. It's great.

sk 说:
2021年8月28日 05:01

I am not surprised that this door company is so hyped in several forums. When I got to see their portfolio, I was awed by how unique their designs are. Visit their https://caldwells.com/ through this link.

merckseo 说:
2021年10月10日 06:59

Best Social Plan is the most trusted company for providing social media marketing services. Within some years it has gained popularity as a reliable company to its customers. This company is now managing its activity successfully. It is proved number one company by its quality services. It is noticed that more and more customers are showing their intention to take service from this company. Best Social Plan

Uttarakhand Board 9t 说:
2022年8月16日 01:28

Uttarakhand Board Model Paper 2023 Class 9 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams

alyssa 说:
2022年11月23日 17:38

Rails 4.0.4 introduced a breaking change wherein ActiveModel::ForbiddenAttributesError is raised when Atlantis Park attempting to mass-assign protected attributes. This was not the case in previous versions of Rails. The fix is to either whitelist the attributes that can be mass-assigned, or to switch to the new strong_parameters gem.

Taxi Cab Service 说:
2023年8月27日 23:56

Tour Saint Lucia offers convenient taxi cab service to ensure seamless transportation during your stay. Our reliable taxi cab service guarantees comfort and efficiency, allowing you to explore the island with ease. Experience hassle-free travel as our experienced drivers take you to your desired destinations.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter