简体   繁体   English

Rails 3路由错误,用户类与Devise :: OmniauthCallbacksController

[英]Rails 3 routing error, User class with Devise::OmniauthCallbacksController

I'm trying to create a profile page for my users, but I get stuck as soon as the user logs in, due to routing error. 我正在尝试为我的用户创建一个配置文件页面,但是由于路由错误,我会在用户登录后立即卡住。 Even if I try to load the first page, I get the same problem again. 即使我尝试加载第一页,我也会再次遇到同样的问题。 When I do a rake routes, I get these user paths: 当我做rake路由时,我得到这些用户路径:

new_user_session GET    /users/sign_in(.:format)               devise/sessions#new
            user_session POST   /users/sign_in(.:format)               devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)              devise/sessions#destroy
 user_omniauth_authorize        /users/auth/:provider(.:format)        users/omniauth_callbacks#passthru {:provider=>/facebook|twitter/}
  user_omniauth_callback        /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:facebook|twitter)
           user_password POST   /users/password(.:format)              devise/passwords#create
       new_user_password GET    /users/password/new(.:format)          devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)         devise/passwords#edit
                         PUT    /users/password(.:format)              devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                devise/registrations#cancel
       user_registration POST   /users(.:format)                       devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)               devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                  devise/registrations#edit
                         PUT    /users(.:format)                       devise/registrations#update
                         DELETE /users(.:format)                       devise/registrations#destroy

users GET    /users(.:format)                       users#index
                         POST   /users(.:format)                       users#create
                new_user GET    /users/new(.:format)                   users#new
               edit_user GET    /users/:id/edit(.:format)              users#edit
                    user GET    /users/:id(.:format)                   users#show
                         PUT    /users/:id(.:format)                   users#update
                         DELETE /users/:id(.:format)                   users#destroy

This is what my Users::OmniauthCallbacksController looks like: 这就是我的Users :: OmniauthCallbacksController的样子:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

I'm not sure about this, but I thought I should create a UsersController in which I could implement methods outside of the device. 我不确定这一点,但我想我应该创建一个UsersController,我可以在其中实现设备之外的方法。 This is what it looks like: 这就是它的样子:

class UsersController < ApplicationController

    def index
        @users = User.all

        respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @ingredients }
    end
    end

    def profile
        @user = current_user

        respond_to do |format|
      format.html 
      format.json { render json: @ingredients }
    end
    end

    def show
        @user = User.find(params[:id])

        respond_to do |format|
      format.html 
      format.json { render json: @ingredients }
    end
    end

end

In my routes.rb I have the following users related lines: 在我的routes.rb中,我有以下用户相关的行:

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

Now, If I load the first page while the user is logged in, I get the error: 现在,如果我在用户登录时加载第一页,我收到错误:

Routing Error

No route matches {:action=>"show", :controller=>"users"}"

Any ideas? 有任何想法吗?

Both the "devise users" and the "users" are attempting to take hold of the "/users" routes. “设计用户”和“用户”都试图抓住“/ users”路由。 The easiest way to solve this is to tell devise to use a prefix: 解决这个问题的最简单方法是告诉devise使用前缀:

devise_for :users, 
  :path_prefix => 'auth', 
  :controllers => {
    :omniauth_callbacks => "users/omniauth_callbacks" 
  }
resources :users

Now the devise authentication will post to "auth_users", and your CRUD will still do to "/users" 现在设计认证将发布到“auth_users”,你的CRUD仍然会对“/ users”做

Here's an example routes.rb file when using Devise along with a custom Users Controller. 以下是使用Devise和自定义用户控制器时的routes.rb文件示例。 I was having trouble with some paths colliding, where I wanted requests to go to my Users controller, but they were going to Devise instead. 我遇到了一些路径冲突的问题,我想要请求转到我的用户控制器,但是他们要去设计。

I had some specific issues with the registration URLs. 我在注册网址上遇到了一些具体问题。 You might either have to do something similar for the URLs you have trouble with or you might be able to get by with just the last line specifying your custom Users controller. 您可能要么针对遇到问题的URL执行类似的操作,要么只需指定自定义用户控制器的最后一行即可。

Railsappname::Application.routes.draw do
  root :to => "home#index"
 
  devise_for :users, :skip => [:sessions, :registrations]
 
  devise_scope :user do
    # make some pretty URLs
    get "login" => "devise/sessions#new", :as => :new_user_session
    post 'login' => 'devise/sessions#create', :as => :user_session
    delete "logout" => "devise/sessions#destroy", :as => :destroy_user_session
    # rewrite the registrations URLs so they don't collide with my custom Users Controller
    get "signup" => "devise/registrations#new", :as => :new_user_registration
    put "update-registration" => "devise/registrations#update", :as => :update_user_registration
    delete "delete-registration" => "devise/registrations#destroy", :as => :delete_user_registration
    get "edit-registration" => "devise/registrations#edit", :as => :edit_user_registration
    get "cancel-registration" => "devise/registrations#cancel", :as => :cancel_user_registration
    post "create-registration" => "devise/registrations#create", :as => :user_registration
  end
 
  resources :users, :controller => "users"
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM