设计认证。 gem signout不能像我预期的那样工作

时间:2012-05-01 23:38:17

标签: ruby-on-rails web-applications login devise

我将Devise集成到我的应用程序中,这里是我认为对此问题很重要的当前代码。

这是我的退出链接

<nav class="round">
        <ul>
            <li>
                <%= link_to "Home", root_path%>
            </li>
            <li>
                <%= link_to "Help (learn Bunch-It)", help_path %>
            </li>
            <% if signed_in? %>
            <li>
                <%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
            </li>
            <li>
                <%= link_to "Profile "+current_user.user_name, user_path(current_user), :method => :delete %>
            </li>
            <%else%>
            <li>
                <%= link_to "Sign in", new_user_session_path %>
            </li>
            <%end%>
        </ul>
    </nav>

然后是route.rb

devise_for :users

  #get "sessions/new"

  #get "session/new"

  resources :users
  #resources :sessions, :only => [:new, :create, :destroy]

  #get "users/new"

  get "pages/home"

  get "pages/contact"

  get "pages/about"

  get "pages/help"

  get "results/result"

  get 'tree/insertResult' => 'tree#insertResult'

  get "tree/deleteFolder" => 'tree#deleteFolder'

  get "tree/createFolder" => 'tree#createFolder'

  get "tree/deleteResult" => 'tree#deleteResult'

  match '/results' , :to => 'results#result'
  match '/contact' , :to => 'pages#contact'
  match '/about' , :to => 'pages#about'
  match '/help' , :to => 'pages#help'
  match '/home_index', :to => 'pages#home'
  #match '/signin', :to => 'sessions#new'
  #match '/signout', :to => 'sessions#destroy'
  root :to => 'pages#home'

这是我的控制器功能。主页

class PagesController < ApplicationController

  def home
    @title = "Home"
    session[:return_to] = request.fullpath
    @page = request.path
    puts "Welcome home page"
  end
...

最后这是我的个人资料网站控制器

class UsersController < ApplicationController
  before_filter :authenticate_user!, :only => :token

  def index

  end
  def token

  end
  def show
    @title = current_user.user_name
    @haveQuery = false
    if @newest_query = findNewestQuery
      @common_query = commonQuery
      @haveQuery = true
    else
      @haveQuery = false
    end
  end

问题是,当我按下退出我的应用程序时,只需刷新个人资料网站或将我引导到个人资料网站,如果我当前在不同的网站上。但是,我希望被定向到我的应用程序的主页而不是个人资料页面。我做了我能想到的但无法解决的事情。任何小小的想法对我来说都像礼物? :)

1 个答案:

答案 0 :(得分:4)

我的rails 3.2应用程序遇到了同样的问题。我发现问题出在我的路径文件中。如果你仍然遇到问题,我建议打开设计宝石并四处寻找。

  devise_for :users, :controllers => {:sessions => "user/sessions", :registrations => "user/registrations"}
  devise_scope :user do
    get '/login' => 'devise/sessions#new'
    get '/logout' => 'devise/sessions#destroy'
  end
  resources :user, :controller => "user"
  root :to => "home#index"

在视图中有一些像这样的东西

<%= link_to 'Logout', logout_path, :method => :delete %>

用户模型看起来像这样

    class User
      # Include default devise modules. Others available are:
      # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable

      # Validations.
      validates_presence_of :email, :first_name, :last_name, :password
      validates_uniqueness_of :email, :case_sensitive => false
      attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me


     end

编辑: 检查那些东西的好方法

$ rake routes
DEPRECATION WARNING: ActiveSupport::Memoizable is deprecated and will be removed in future releases,simply use Ruby memoization pattern instead. (called from <top (required)> at /Users/telmate/Documents/tmp_H/hucker/config/application.rb:20)
MongoDB logging. Please note that logging negatively impacts performance and should be disabled for high-performance production apps.
MONGODB admin['$cmd'].find({:ismaster=>1}).limit(-1)
        new_user_session GET    /users/sign_in(.:format)       user/sessions#new
            user_session POST   /users/sign_in(.:format)       user/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      user/sessions#destroy
           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)        user/registrations#cancel
       user_registration POST   /users(.:format)               user/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       user/registrations#new
  edit_user_registration GET    /users/edit(.:format)          user/registrations#edit
                         PUT    /users(.:format)               user/registrations#update
                         DELETE /users(.:format)               user/registrations#destroy
                   login GET    /login(.:format)               devise/sessions#new
                  logout GET    /logout(.:format)              devise/sessions#destroy
              user_index GET    /user(.:format)                user#index
                         POST   /user(.:format)                user#create
                new_user GET    /user/new(.:format)            user#new
               edit_user GET    /user/:id/edit(.:format)       user#edit
                    user GET    /user/:id(.:format)            user#show
                         PUT    /user/:id(.:format)            user#update
                         DELETE /user/:id(.:format)            user#destroy
                    root        /                              home#index

这里也是我的控制器,所以我们大多在同一页面上。

class User::SessionsController < Devise::SessionsController

  def new
    super
  end

  def create
     resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
     if is_navigational_format?
       if resource.sign_in_count == 1
         set_flash_message(:notice, :signed_in_first_time)
       else
         set_flash_message(:notice, :signed_in)
       end
     end
     sign_in(resource_name, resource)
     respond_with resource, :location => redirect_location(resource_name, resource)
   end  
end

class User::RegistrationsController < Devise::RegistrationsController

  def new
      super # no customization, simply call the devise implementation
    end

    def create
      begin
        super # this calls Devise::RegistrationsController#create
      rescue MyApp::Error => e
        e.errors.each { |error| resource.errors.add :base, error }
        clean_up_passwords(resource)
        respond_with_navigational(resource) { render_with_scope :new }
      end
    end

    def update
      super # no customization, simply call the devise implementation 
    end

    protected

    def after_sign_up_path_for(resource)
      new_user_session_path
    end

    def after_inactive_sign_up_path_for(resource)
      new_user_session_path
    end
end