OmniAuth Railscast中的DangerousAttributeError教程:create由ActiveRecord定义

xta*_*xta 8 ruby activerecord ruby-on-rails railscasts omniauth

我在SO上看过ActiveRecord :: DangerousAttributeError和其他类似的线程,但它们没有解决同样的问题.

我正在关注omniauth教程:http://railscasts.com/episodes/235-omniauth-part-1?view = ascicast

我可以使用Twitter通过oauth进行身份验证并返回用户的数据(auth).问题是由于此错误消息,我无法在数据库(sqlite3)中创建/保存它.

错误:

ActiveRecord::DangerousAttributeError in AuthenticationsController#create

create is defined by ActiveRecord
Rails.root: /beta/devise-omniauth1

Application Trace | Framework Trace | Full Trace
app/controllers/authentications_controller.rb:15:in `create'
Run Code Online (Sandbox Code Playgroud)

Authentications_Controller:

  def create
    auth = request.env["omniauth.auth"] 
    current_user.authentications.create(:provider => auth['provider'], :uid => auth['uid'])
    flash[:notice] = "Authentication successful."
    redirect_to authentications_url
  end
Run Code Online (Sandbox Code Playgroud)

楷模:

class Authentication < ActiveRecord::Base
belongs_to :user
end


class User < ActiveRecord::Base
has_many :authentications

  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :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
end
Run Code Online (Sandbox Code Playgroud)

我如何处理此错误?谷歌搜索这个网站和其他人并没有帮助我了解正在发生的事情,以解决它.谢谢

小智 8

我刚刚遇到了同样的RailsCast.

该教程说要运行:

rails g nifty:scaffold authentication user_id:integer \
        provider:string uid:string index create destroy
Run Code Online (Sandbox Code Playgroud)

但是我的机器上没有漂亮的脚手架,我只是跑了

rails g scaffold authentication user_id:integer \
        provider:string uid:string index create destroy
Run Code Online (Sandbox Code Playgroud)

其表现不同.它不是创建存根'index','create'和'destroy'控制器方法,而是在数据库中创建字段.

如前所述,删除它们并且工作正常.


Fre*_*ung 4

Activerecord 警告您某些数据库属性名称(创建等)与 activerecord/ruby 提供的实例方法名称冲突。

由于 Rails 会创建这些名称的实例方法来访问属性,因此这种冲突常常会导致非常奇怪的事情发生。因此,活动记录会引发异常,警告您正在发生这种情况