无法批量分配受保护的属性:provider,uid(omniauth railcast)

时间:2013-04-10 07:55:51

标签: ruby-on-rails ruby ruby-on-rails-3 omniauth

我正在密切关注omniauth的railscast但是我收到了视频中没有遇到的错误

Can't mass-assign protected attributes: provider, uid

这是我创建的身份验证控制器

class AuthenticationsController < InheritedResources::Base

def index
    @authentications = current_user.authentications if current_user
end

def create
  omniauth = request.env['omniauth.auth']
  authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
  if authentication
     flash[:notice] = "Signed in successfully"
     sign_in_and_redirect(:user, authentication.user)
  elsif current_user
   current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
   flash[:notice] = "Authentication successful"
   redirect_to authentications_url
   else
    user = User.new
    user.apply_omniauth(omniauth)
    user.save(:validate=>false)
    flash[:notice] = "Signed in successfully"
     sign_in_and_redirect(:user, user)
end
end

def destroy
    @authentication = current_user.authentications.find(params[:id])
    @authentication.destroy
    flash[:notice] = "Successfully destroyed authentication"
    redirect_to authentications_url

end
end

以下是我在user.rb中所添加的内容:在查看过去的帖子之后,我添加了:provider和:uid到attr_accessible

class User < ActiveRecord::Base

  attr_accessible :name, :email, :password, :password_confirmation, :provider, :uid
  has_many :authentications

不幸的是,当我尝试使用Twitter登录时,我仍然收到此错误(path = / auth / twitter / callback)

1 个答案:

答案 0 :(得分:0)

你应该添加

attr_accessible :provider, :uid

在Authentications模型中,而不是在User模型中。

(因为这些是认证模型的属性,您在认证模型中大量分配它)