gpt4 book ai didi

ruby-on-rails - 没有路由匹配 [GET] "/auth/twitter"OmniA

转载 作者:行者123 更新时间:2023-12-03 18:25:01 25 4
gpt4 key购买 nike

我没有使用设计或其他一些类似的 gem 。我对 RoR 很陌生。
这是我的routes.rb

# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

Rails.application.routes.draw do
get "about", to: "about#index"

get "password", to: "passwords#edit", as: :edit_password
patch "password", to: "passwords#update"

get "password/reset", to: "password_resets#new"
post "password/reset", to: "password_resets#create"
get "password/reset/edit", to: "password_resets#edit"
patch "password/reset/edit", to: "password_resets#update"

get '/auth/:provider/callback', to: 'sessions#create'

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"

root to: "main#index"
end

这是 user.rb
# email:string
# password_digest:string
#
# password:string virtual
# password_confirmation:string virtual

class User < ApplicationRecord
has_secure_password

validates :email, presence: true, format: { with: /\A[^@\s]+@[^@\s]+\z/, message: "must be a valid email address" }

end
这是我的 omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter,Rails.application.credentials.dig(:twitter,:api_key), Rails.application.credentials.dig(:twitter,:api_key)
end
我已经在我的 Twitter 应用程序中进行了所有设置。请帮忙。

最佳答案

我是 Ruby on Rails 初学者类(class)的作者。 👋 我已经更新了视频以反射(reflect)这些变化。
Omniauth 2.0 已发布,它要求您现在使用 POST 请求以确保安全。
现在我们将添加两个 gem :

bundle add omniauth-twitter omniauth-rails_csrf_protection
并确保您有 api_secret作为 omniauth.rb 初始值设定项中的第二个参数:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter,Rails.application.credentials.dig(:twitter,:api_key), Rails.application.credentials.dig(:twitter,:api_secret)
end
然后你可以通过添加 method: :post 重定向到 Twitter。给您的 link_tobutton_to
link_to "Connect Twitter", "/auth/twitter", method: :post, class: "btn btn-primary"
button_to "Connect Twitter", "/auth/twitter", method: :post, class: "btn btn-primary"
这适用于项目和独立 Twitter 应用程序,因此您可以使用任何一个。 👍

关于ruby-on-rails - 没有路由匹配 [GET] "/auth/twitter"OmniA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66009147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com