Create named routes for OmniAuth in Rails 3

前端 未结 3 1612
别跟我提以往
别跟我提以往 2021-02-05 22:04

After having watched Ryan\'s excellent Railcast Simple OmniAuth, I\'ve managed to implement authentication in my app.

Everything is working fine, but in my view I have l

相关标签:
3条回答
  • 2021-02-05 22:28

    With Rails 3 you can do :

    # routes.rb
    match '/signin/:provider/callback' => 'sessions#create', :as => :signing
    
    #view.erb
    <%= link_to 'twitter', signing_path(:provider => 'twitter') %>
    <%= link_to 'facebook', signing_path(:provider => 'facebook') %>
    
    0 讨论(0)
  • 2021-02-05 22:45

    Notice that in link_to, you're just providing a string for the route argument. So you can just define a method in a helpers file.

    # application_helper.rb
    module ApplicationHelper
      def signin_path(provider)
        "/auth/#{provider.to_s}"
      end
    end
    
    # view file
    <%= link_to 'Sign in with Twitter', signin_path(:twitter) %>
    

    If you want to get all meta

    # application_helper.rb
    module ApplicationHelper
      def method_missing(name, *args, &block)
        if /^signin_with_(\S*)$/.match(name.to_s)
          "/auth/#{$1}"
        else
         super
        end
      end
    end
    
    #view file
    <%= link_to 'Sign in with Twitter', signin_with_twitter %>
    
    0 讨论(0)
  • 2021-02-05 22:50

    Add this to your routes.rb

    get "/auth/:provider", to: lambda{ |env| [404, {}, ["Not Found"]] }, as: :oauth

    Now you can use oauth_path url helper to generate urls.

    Eg. oauth_path(:facebook) # => /auth/facebook

    0 讨论(0)
提交回复
热议问题