0

我正在尝试使用 Rails 4.0.0 设置 Devise 3.1.0。

我已经像这样配置了我的路由器:

devise_for :users,
  :controllers => {
    :registrations => 'users/registrations',
    :sessions => 'users/sessions'
  },
  :path_names => {
    :sign_in => 'login',
    :sign_out => 'logout',
    :sign_up => 'new'
  }


        new_user_session GET    /users/login(.:format)         users/sessions#new
            user_session POST   /users/login(.:format)         users/sessions#create
    destroy_user_session DELETE /users/logout(.:format)        users/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
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        users/registrations#cancel
       user_registration POST   /users(.:format)               users/registrations#create
   new_user_registration GET    /users/new(.:format)           users/registrations#new
  edit_user_registration GET    /users/edit(.:format)          users/registrations#edit
                         PATCH  /users(.:format)               users/registrations#update
                         PUT    /users(.:format)               users/registrations#update
                         DELETE /users(.:format)               users/registrations#destroy

我还打开了作用域视图,并覆盖了默认视图和注册控制器:

  # config/initializers/devise.rb
  config.scoped_views = true

  rails generate devise:views users

  # app/controllers/users/registrations_controller.rb
  #
  # NOTE: I created this class, so creating new users could only be done by authenticated users.
  #
  class Users::RegistrationsController < Devise::RegistrationsController
    before_filter :authenticate_user!
    prepend_before_filter :authenticate_scope!
    skip_before_filter :require_no_authentication
  end

  # app/controllers/users/sessions_controller.rb
  # (currently empty)
  class Users::SessionsController < Devise::SessionsController
  end

问题:

为什么设计:path_names在某些情况下会忽略上述设置?

例如,此方法不会使用:sign_in上面的设置,并返回错误的路径:

new_session_path(resource_name)
=> /users/sign_in

resource_name
=> user

而此方法返回正确的路径:

new_user_session_path
=> /users/login

问题是,Devise 内部使用前一种方法,并且在用户未登录时不断重定向到错误的路径。

我是否配置错误,或者设计工作不正常?这可能是 Rails 4 的问题吗?

4

2 回答 2

0

我认为您试图使用错误的功能来解决您的问题,在此链接中向您展示如何为设计编写另一条路线,在我的应用程序中,我使用以下方法重新创建了路线:

  devise_scope :user do
    get '/cadastrar' => 'devise/registrations#new'
    get '/entrar' => 'devise/sessions#new'
    get '/editar' => 'devise/registrations#edit'
    delete '/sair' => 'devise/sessions#destroy'
  end

如果工作我很高兴!

于 2014-11-04T11:38:57.180 回答
0

我不确定我遇到的问题是什么。(如果有人知道,我仍然有兴趣知道)。

但是我找到了另一种方式来表达我想要的设计,它似乎工作正常:

devise_for :users,
  :controllers => {
    :registrations => 'users/registrations'
  },
  :path_names => {
    :sign_up => 'new'
  }, :skip => [:sessions]
  as :user do
    get '/users/login' => 'devise/sessions#new', :as => :new_user_session
    post '/users/login' => 'devise/sessions#create', :as => :user_session
    match '/users/logout' => 'devise/sessions#destroy', :as => :destroy_user_session,
      :via => Devise.mappings[:user].sign_out_via
  end
于 2013-09-08T21:42:03.480 回答