简体   繁体   English

::加载ActiveModel :: MassAssignmentSecurity错误

[英]ActiveModel::MassAssignmentSecurity::Error

Hi I am using devise and omniauth to autenticate facebook login, but I get the following error: 嗨,我正在使用devise和omniauth来验证facebook登录,但是我收到以下错误:

Can't mass-assign protected attributes: token
app/models/user.rb:20:in `apply_omniauth'
app/controllers/authentications_controller.rb:19:in `create'

this the user model: 这个用户模型:

class User < ActiveRecord::Base

  # The relationship between the User and Authentication model
  has_many :authentications, :dependent => :delete_all

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me,
  # attr_accessible :title, :body

  def apply_omniauth(auth)
    # In previous omniauth, 'user_info' was used in place of 'raw_info'
    self.email = auth['extra']['raw_info']['email']
    authentications.build(:provider => auth['provider'], :uid => auth['uid'], :token => auth['credentials']['token'])
  end

end

This is my authentication controller: 这是我的身份验证控制器:

class AuthenticationsController < ApplicationController
  def index
    @authentications = current_user.authentications if current_user
  end

  def create
    auth = request.env["omniauth.auth"]

    # Try to find authentication first
    authentication = Authentication.find_by_provider_and_uid(auth['provider'], auth['uid'])

    if authentication
      # Authentication found, sign the user in.
      flash[:notice] = "Signed in successfully."
      sign_in_and_redirect(:user, authentication.user)
    else
      # Authentication not found, thus a new user.
      user = User.new
      user.apply_omniauth(auth)
      if user.save(:validate => false)
        flash[:notice] = "Account created and signed in successfully."
        sign_in_and_redirect(:user, user)
      else
        flash[:error] = "Error while creating a user account. Please try again."
        redirect_to root_url
      end
    end 
  end

  def destroy
    @authentication = Authentication.find(params[:id])
    @authentication.destroy
    redirect_to authentications_url, :notice => "Successfully destroyed authentication."
  end
end

Could somebody explain why I get this error, and how I fix it? 有人可以解释为什么我会收到此错误,以及我如何修复它?

:token添加到身份验证模型中的attr_accessible行应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Devise和ActiveModel :: MassAssignmentSecurity :: Error - Devise and ActiveModel::MassAssignmentSecurity::Error Ruby on Rails ActiveModel::MassAssignmentSecurity::Error - Ruby on Rails ActiveModel::MassAssignmentSecurity::Error ActiveModel :: MassAssignmentSecurity ::嵌套属性出错 - ActiveModel::MassAssignmentSecurity::Error with nested attributes ::加载ActiveModel MassAssignmentSecurity - ActiveModel::MassAssignmentSecurity ActiveModel :: MassAssignmentSecurity ::没有控制器的Rails 4出错 - ActiveModel::MassAssignmentSecurity::Error with Rails 4 without a controller UsersController#create中的ActiveModel :: MassAssignmentSecurity :: Error - ActiveModel::MassAssignmentSecurity::Error in UsersController#create PostsController#update中的ActiveModel :: MassAssignmentSecurity :: Error - ActiveModel::MassAssignmentSecurity::Error in PostsController#update PhonesController#create中的ActiveModel :: MassAssignmentSecurity :: Error - ActiveModel::MassAssignmentSecurity::Error in PhonesController#create SchedulesController#create中的ActiveModel :: MassAssignmentSecurity :: Error - ActiveModel::MassAssignmentSecurity::Error in SchedulesController#create 密码,Password_Confirmation ActiveModel :: MassAssignmentSecurity :: Error: - Password, Password_Confirmation ActiveModel::MassAssignmentSecurity::Error:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM