【问题标题】:connecting Omniauth with twitter将 Omniauth 与 Twitter 连接
【发布时间】:2021-06-02 18:35:39
【问题描述】:

我正在关注这一系列 Rails 教程。 现在,在按照本教程将用户连接到 Twitter 时,我陷入了困境 https://www.youtube.com/watch?v=o48-i4m6W78&feature=emb_title&ab_channel=GoRails .连接到 twitter 工作正常,但数据库中没有用户的活动记录,就像在视频中解释的那样,你可以在 7:32 由讲师看到,讲师数据库有 user.twitter_accounts 的活动记录,但我的给了我 nil 和它如图所示,根本没有活动记录。我尝试了不同的方法,但似乎都不起作用,所以我希望我的问题清晰简单,问候。

下面的代码: omn​​iauth_callbacks_controller.rb

class OmniauthCallbacksController < ApplicationController
  def twitter
    Current.user.twitter_accounts.create(
      name: auth.info.name,
      username: auth.info.nickname,
      image: auth.info.image,
      token: auth.credentials.token,
      secret: auth.credentials.secret,
    )

    redirect_to root_path, notice: "Successfully connected your account"
  end

  def auth
    request.env["omniauth.auth"]
  end
end

twitter_accounts_controller.rb

class TwitterAccountsController < ApplicationController
  def index
    @twitter_accounts = Current.user.twitter_accounts
  end
end

当前.rb

class Current < ActiveSupport::CurrentAttributes
  attribute :user
end

twitter_account.rb

class TwitterAccount < ApplicationRecord
  belongs_to :user
end

用户.rb


class User < ApplicationRecord
  has_many :twitter_accounts
  has_secure_password

  #To validate and prevent from making email empty
  validates :email, presence: true, format: { with: /\A[^@\s]+@[^@\s]+\z/, message: "must be a vaild email address" }
end

https://ibb.co/74T6GNn

【问题讨论】:

  • 你能提供一些代码吗?还是我们应该重新观看讲师的视频?你试过先问导师吗?
  • 我刚刚提供了上面的代码。

标签: ruby-on-rails twitter omniauth ruby-on-rails-6


【解决方案1】:

我通过创建一个新用户然后连接到 twitter 解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    相关资源
    最近更新 更多