Please use new recommended protection model for params(strong_parameters) or add `protected_attributes` to your gemfile

后端 未结 1 485
借酒劲吻你
借酒劲吻你 2021-01-03 04:13

This happened when I added an attr_accessible to my Relationship model.

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id
end
         


        
1条回答
  •  别那么骄傲
    2021-01-03 05:16

    In Rails 4 you use Strong Parameters instead of Protected Attributes. (You don't need to include the gem in your gemfile as it's already included.)

    You take the Rails 3 attr_accessible code out of your model and put corresponding code into your controller. See here for more documentation: https://github.com/rails/strong_parameters

    In your case, something like:

    class RelationshipController < ActionController::Base
      def create
        @relationship = Relationship.new(relationship_params)
    
        if @relationship.save
            # do something
        else
            # do something
        end
      end
    
      private
        def relationship_params
          params.require(:relationship).permit(:followed_id)
        end
    end
    

    Edit:

    Here's a good article I just came across about this: http://blog.sensible.io/2013/08/17/strong-parameters-by-example.html

    0 讨论(0)
提交回复
热议问题