2013-02-17 43 views
1

我試圖爲我的用戶創建一個配置文件頁面,但由於路由錯誤,一旦用戶登錄就卡住了。即使我嘗試加載第一頁,我也會再次遇到同樣的問題。當我用耙子的路線,我得到這些用戶的路徑:Rails 3路由錯誤,用戶類與Devise :: OmniauthCallbacksController

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 

這是我的用戶:: 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 

我不知道這一點,但我想我應該創建一個UsersController,我可以在其中實現設備外部的方法。這是什麼樣子:

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 

在我的routes.rb中,我有以下的用戶相關線路:

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

現在,如果我打開的第一個頁面,而用戶登錄,我得到錯誤:

Routing Error 

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

任何想法?

+0

您重新啓動您的服務器? – Edward 2013-02-17 13:32:39

+0

嗯...以及我沒有清除服務器chache,但是,我重新啓動了它。 – Majoren 2013-02-17 13:34:29

回答

2

這裏是一個示例routes.rb文件時使用設計與自定義用戶控制器一起使用。我遇到了一些路徑碰撞的問題,我想要將請求發送到我的用戶控制器,但他們要去設計。

我對註冊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 
2

「設計用戶」和「用戶」都試圖抓住「/用戶」路線。要解決這個問題,最簡單的方法是告訴設計使用前綴:

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

現在色器件認證將張貼到「auth_users」,和你的CRUD仍然會做「/用戶」