Rails 4.0.4 ActiveModel::ForbiddenAttributesError

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

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


登录 *


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