我设置了一个单独的注册和登录系统。我正在使用带有rails 6的omniauth2构建一个应用程序,让用户安排推文发布。我正在使用omniauth2,因此用户可以授权应用程序访问他们的帐户
我想从omniauth2数据中获取用户的用户名和头像,但是当用户授权时它没有显示任何内容,我将发布相关的路由、控制器和视图代码
nil出现在我想要显示的类别的数据库中
控制台输出
irb(main):002:0> User.last.twitter_accounts
id: 6,
user_id: 7,
name: nil,
username: nil,
image: nil,
token: "[FILTERED]",
secret: "[FILTERED]",
created_at: Mon, 06 Dec 2021 01:56:09.804716000 UTC +00:00,
updated_at: Mon, 06 Dec 2021 01:56:09.804716000 UTC +00:00
路线文件
Rails.application.routes.draw do
get "about", to: "about#index", as: :about
get "password", to: "passwords#edit", as: :edit_password
patch "password", to: "passwords#update"
get "sign_up", to: "registrations#new"
post "sign_up", to: "registrations#create"
get "sign_in", to: "sessions#new"
post "sign_in", to: "sessions#create"
delete "logout", to: "sessions#destroy"
get "password/reset", to: "password_resets#new"
post "password/reset", to: "password_resets#create"
get "password/reset/edit", to: "password_resets#edit"
post "password/reset/edit", to: "password_resets#update"
get "/auth/twitter/callback", to: "omniauth_callbacks#twitter"
resources :twitter_accounts
root to: "main#index"
end
控制器文件
class OmniauthCallbacksController < ApplicationController
def twitter
twitter_account = Current.user.twitter_accounts.where(username: auth.info.nickname).first_or_initialize
twitter_account.update(
name: auth.info.name,
image: auth.info.image,
token: auth.credentials.token,
secret: auth.credentials.secret,
)
redirect_to twitter_accounts_path, notice: "Twitter account was successfully connected"
end
def auth
request.env["omniauth.auth"]
end
end
视图文件
```推特账号
<% @twitter_accounts.each 做 |twitter_account| %>