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

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

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

我可以通过oauth通过Twitter进行身份validation并返回用户的数据(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' 

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 

楷模:

 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 

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

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

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

我刚刚在同一个RailsCast中遇到了这个问题。

该教程说要运行:

 rails g nifty:scaffold authentication user_id:integer \ provider:string uid:string index create destroy 

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

 rails g scaffold authentication user_id:integer \ provider:string uid:string index create destroy 

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

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

因此,只需完成问题,您将需要使用此命令创建迁移:

 rails g migration remove_silly_authentication_fields_which_should_not_be_there 

看起来像这样:

 class DropSillyControllerAttributes < ActiveRecord::Migration def change remove_column :authentications, :index remove_column :authentications, :create remove_column :authentications, :destroy end end 

并使用通常的方式运行它:

 rake db:migration 

或者你应该能够运行:

 rake db:rollback 

回滚刚刚对数据库所做的更改,并且:

 rails d scaffold authentication 

要删除所有文件,请运行:

 rails g scaffold authentication user_id:integer provider:string uid:string 

并手动完成其他事情

顺便说一下,我自己做了同样的事情。

尝试:current_user.authentications.create!

编辑

所以基本上你的问题是你的表中的列名称与Modal类的方法相同。

您的数据库中不能包含名为create或destroy的列。

很可能是你的模型/控制器生成错字。