现在的位置: 首页 > 综合 > 正文

No more attr_accessible in Ruby on Rails 4

2013年09月16日 ⁄ 综合 ⁄ 共 849字 ⁄ 字号 评论关闭

In a file called list.rb, I had

  attr_accessible :title, :user_id, :updated_at, :created_at   

When you run the tests using 'edge' Rails (4.0.0 beta), you get an error like this: "`attr_accessible` is extracted out of Rails into a gem. Please use new recommended protection model for params(strong_parameters) or add
`protected_attributes` to your Gemfile to use old one.
"

What does this mean? This blog tells you:

http://logicalfriday.com/2012/10/15/sneaking-a-peak-at-rails-4/

Remove the attr_accessible line from your models. In each of the controllers, add at the bottom something like this (for example, this is
list_controller):

 private
  def app_params
    params.require(:list).permit(:title, :user_id, :updated_at, :created_at)
  end
and, where you create a new list in the controller (the
create method), instead of
@list = List.create(params[:list])

 

you should have

@list = List.create( app_params )

转自:

http://rubyjunction.us/no-more-attr-accessible-in-ruby-on-rails-4

抱歉!评论已关闭.