【问题标题】:DangerousAttributeError in OmniAuth Railscast Tutorial: create is defined by ActiveRecordOmniAuth Railscast 教程中的 DangerousAttributeError:create 由 ActiveRecord 定义
【发布时间】:2012-04-19 03:30:43
【问题描述】:

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

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

我能够通过 oauth 与 Twitter 进行身份验证并返回用户的数据 (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

如何解决此错误?在这个网站和其他网站上搜索并不能帮助我理解为了修复它发生了什么。谢谢

【问题讨论】:

  • 用户和身份验证模型有哪些列
  • 身份验证:user_id、provider、uid、index、create、destroy 谢谢,我更新了迁移,从表中取出最后 3 列后错误得到解决
  • @XTA 我遇到了完全相同的问题,您是通过什么方式从表中删除最后 3 列的?我是个新手 =)

标签: ruby-on-rails ruby activerecord omniauth railscasts


【解决方案1】:

我刚刚在同一个 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”控制器方法,而是在数据库中创建字段。

如前所述,删除它们即可正常工作。

【讨论】:

  • 我只是遇到了同样的问题——删除这些字段的最佳方法是什么?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    因此,为了完成问题,您需要使用以下命令创建迁移:

    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
    

    然后手动做其他事情

    顺便说一句,我自己也做过同样的事情。

    【讨论】:

    • 谢谢!我刚刚遇到了同样的问题,尽管你能澄清一下你到底写了什么:“rails g migration remove_silly_authentication_fields_which_should_not_be_there”
    • @Apane101 这应该是您创建的迁移的名称不正确。
    【解决方案4】:

    试试:current_user.authentications.create!

    编辑

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

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

    这很可能是您的模型/控制器生成的拼写错误。

    【讨论】:

    • 使用current_user.authentications.create!(:provider =&gt; auth['provider'], :uid =&gt; auth['uid'])后还是报错:ActiveRecord::DangerousAttributeError in AuthenticationsController#create create is defined by ActiveRecord
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    相关资源
    最近更新 更多