OmniauthCallbacksController中的LoadError#passthru(带有设计/条带连接)

时间:2017-02-04 20:35:37

标签: ruby-on-rails devise stripe-connect

尝试实施Stripe Connect,当我点击" connect to stripe"时出现以下错误:按钮。

行动' passthru'无法找到OmniauthCallbacksController

用户/ omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
    def stripe_connect
        @user = current_user
        if @user.update_attributes({
          provider: request.env["omniauth.auth"].provider,
          uid: request.env["omniauth.auth"].uid,
          access_code: request.env["omniauth.auth"].credentials.token,
          publishable_key: request.env["omniauth.auth"].info.stripe_publishable_key
        })
          # anything else you need to do in response..
          sign_in_and_redirect @user, :event => :authentication
          set_flash_message(:notice, :success, :kind => "Stripe") if is_navigational_format?
        else
          session["devise.stripe_connect_data"] = request.env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
      end
end

模型/ user.rb

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:stripe_connect]

的routes.rb

devise_for :users, controllers: { registrations: 'users/registrations', :omniauth_callbacks => "users/omniauth_callbacks" }

gemfile.rb

gem 'omniauth-stripe-connect'

初​​始化/ stripe.rb

Rails.configuration.stripe = {
  :publishable_key => ENV['PUBLISHABLE_KEY'],
  :secret_key      => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]

初​​始化/ devise.rb

config.omniauth :stripe_connect,
  ENV['STRIPE_CONNECT_CLIENT_ID'],
  ENV['STRIPE_SECRET_KEY'],
  :scope => 'read_write',
  :stripe_landing => 'register'

按钮链接:

<%= link_to image_tag('blue-on-light.png'), user_stripe_connect_omniauth_authorize_path(:stripe_connect) %>

正如我对我的微博Ruby心灵的理解,我需要定义&#39; passthru&#39;?我该怎么定义呢?当我进入时:

def passthru
end

链接无效/页面重新加载。避风港能够在这里找到解决方案。我错过了什么?

编辑:

将我的连接更改为条带链接:

 <%= link_to image_tag('blue-on-light.png'), "/users/auth/stripe_connect" %>

该链接将我带到连接到条带页面,但是当我点击&#34;连接到条带&#34;按钮,页面无法找到,并且无法加载或重定向。

1 个答案:

答案 0 :(得分:1)

您可以尝试更改

吗?
# app/controllers/omniauth_callbacks_controller.rb
class OmniauthCallbacksController < ApplicationController
    def stripe_connect
    ....

class OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def stripe_connect
      @user = User.find_for_stripe_connect(request.env['omniauth.auth'], current_user)
      set_notice_and_redirect
    end

    private

    def set_notice_and_redirect          
      if @user.persisted?
          flash[:notice] = 'Successfully signed in'
          set_flash_message(:notice, :success, :kind => "Stripe") if is_navigational_format?
        else
          session["devise.stripe_connect_data"] = request.env["omniauth.auth"]
          redirect_to new_user_registration_url
      end
    end
end

并在您的用户模型中

# Checks if user exists, otherwise create it
def self.find_for_stripe_connect(access_token, _ = nil)
  data = access_token.info
  user = User.where(email: data['email']).first_or_create(
    email: data['email'],
    password: Devise.friendly_token[0, 20],
    provider: request.env["omniauth.auth"].provider,
    uid: request.env["omniauth.auth"].uid,
    access_code: request.env["omniauth.auth"].credentials.token,
    publishable_key: request.env["omniauth.auth"].info.stripe_publishable_key
  )
  user
end

并在路径中唱歌

<%= link_to image_tag('blue-on-light.png'), user_stripe_connect_omniauth_authorize %>

我认为你不需要定义一个passthru动作。如果您在路线中看到以下两个,它可以工作。授权路径用于将用户重定向到条带,回调用于将用户从条带重定向回您的站点

$ rake routes

user_stripe_connect_omniauth_authorize /auth/stripe_connect(.:format)  ....
user_stripe_connect_omniauth_callback /auth/stripe_connect/callback(.:format) ....