【问题标题】:Devise after_sign_in_path_for works, but not the other ones设计 after_sign_in_path_for 作品,但不是其他作品
【发布时间】:2013-10-18 14:14:38
【问题描述】:

我真的错过了一些东西。

我已经阅读了很多关于设计重定向的内容(对于大多数人来说似乎很难实现......)但就我而言,我真的不明白。

有时我读到方法 after_<action>_path_for(resource) 应该在 ApplicationController 中,有时它被提到在特定控制器中,覆盖设计控制器。

我宁愿将它们放在我的ApplicationController 中,因为它让我为重定向创建更​​多控制器而烦恼,但如果最后不可能,我不会坚持...

这是交易:

我的ApplicationController 中有:(还有其他一些,但对于示例来说已经足够了)

  def after_update_path_for(user)
    flash[:notice] = 'Successfully updated password'
    edit_user_path(user)
  end

  def after_inactive_sign_up_path_for(user)
    flash[:notice] = 'Welcome! Please follow the steps!'
    me_new_path
  end

  def after_sign_up_path_for(user)
    flash[:notice] = 'Welcome! Please follow the steps!'
    me_new_path
  end

  def after_sign_in_path_for(user)
    if user.sign_in_count == 1
      me_new_path
    else
      root_path
    end
  end

而疯狂的是,after_sign_in_path_for 被调用了,而其他的却没有。就像当用户注册时,重定向他的是if user.sign_in_count == 1,而不是after_inactive_sign_up_path_for,也不是after_sign_up_path_for

怎么会?

这可能与我的路线有关,所以这是我的routes.rb 摘录:

  devise_for :user, :skip => [:sessions, :registrations], :path => ''
  devise_scope :user do
    get :register, :to => 'devise/registrations#new'
    post :register, :to => 'devise/registrations#create'
    put :update_password, :to => 'devise/my_registrations#update'
    get :login, :to => 'devise/sessions#new'
    get :login, :to => 'devise/sessions#new', :as => :new_copasser_session
    post :login, :to => 'devise/sessions#create'
    delete :logout, :to => 'devise/sessions#destroy'
  end

我将 Devise 3.1.0 与 Ruby 1.9.3 和 Rails 3.2.13 一起使用

感谢您的帮助!


编辑

感谢@rich-peck 的回答。我以这种方式更新了我的routes.rb

  devise_for :users, :path => '', :path_names => { 
    :sign_in => :login,
    :registration => :register,
    :sign_up => '',
    :sign_out => :logout
  }

这给了我与之前相同的路线(除了我不能再使用 login_path 助手,但这没什么大不了的),但我仍然得到与重定向相同的结果。

这是rake routes的结果:

                       new_user_session GET    /login(.:format)                                               devise/sessions#new
                           user_session POST   /login(.:format)                                               devise/sessions#create
                   destroy_user_session DELETE /logout(.:format)                                              devise/sessions#destroy
                          user_password POST   /password(.:format)                                            devise/passwords#create
                      new_user_password GET    /password/new(.:format)                                        devise/passwords#new
                     edit_user_password GET    /password/edit(.:format)                                       devise/passwords#edit
                                        PUT    /password(.:format)                                            devise/passwords#update
               cancel_user_registration GET    /register/cancel(.:format)                                     devise/registrations#cancel
                      user_registration POST   /register(.:format)                                            devise/registrations#create
                  new_user_registration GET    /register(.:format)                                            devise/registrations#new
                 edit_user_registration GET    /register/edit(.:format)                                       devise/registrations#edit
                                        PUT    /register(.:format)                                            devise/registrations#update
                                        DELETE /register(.:format)                                            devise/registrations#destroy
                      user_confirmation POST   /confirmation(.:format)                                        devise/confirmations#create
                  new_user_confirmation GET    /confirmation/new(.:format)                                    devise/confirmations#new
                                        GET    /confirmation(.:format)                                        devise/confirmations#show

有什么想法吗?

【问题讨论】:

  • 您需要按照设计文档中的规定将覆盖放在正确的控制器中。不是所有的都可以进入ApplicationController。

标签: ruby-on-rails ruby-on-rails-3 devise


【解决方案1】:

Devise 严重依赖一个名为“资源”的中心变量。这个变量定义了设计如何在你的系统上运行,这就是为什么你必须将设计“附加”到 :users 或类似的

人们在使用 Devise 时遇到问题,因为他们不遵守惯例,并且将表单放在任何地方。如果他们阅读了 Devise 自述文件,他们会很感激它非常灵活:)

我相信您的问题与您的路线有关,因为您可能希望将所有这些静态路线合并成这样的东西:

devise_for :users, :path => '', :controllers => {:sessions => 'sessions', :registrations => 'registrations'}, :path_names => { :sign_in => 'login', :password => 'forgot', :confirmation => 'confirm', :unlock => 'unblock', :registration => 'register', :sign_up => 'new', :sign_out => 'logout'}

【讨论】:

  • 我更新了我的routes.rb 并编辑了我的问题。但它仍然行不通。还有什么想法吗?谢谢!
  • 嗯嗯,如果你有时间,可能值得加入聊天?
  • 当然!我该如何开始?
  • 在线时告诉我(只需发表评论),我会创建一个聊天室供我们进入!
  • 平! :)(我在欧洲)
【解决方案2】:

感谢@rich-peck 的帮助,我们搞定了。

问题是为什么after_sign_in_path_for 的行为与after_sign_up_path_for 和 cie 不同?

in the devise sources 看来,after_sign_in_path_for 是在帮助器中定义的,而其他的是其控制器的方法(例如 Devise::RegistrationsController < DeviseController

因此对于after_sign_in_path_for,它可以覆盖ApplicationController,而对于其他的,需要创建一个registrations_controller.rb 文件来覆盖其中的方法:

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(copasser)
    flash[:notice] = 'Welcome! Please follow the steps!'
    me_new_path
  end

end

并以这种方式设置router.rb

devise_for :copassers, :controllers => { 
    :registrations => :registrations
  }

我认为行为是不同的,因为:registerable, :recoverable 等是设计模块,不一定要使用,并且在这种情况下助手不适合。在这一点上,设计贡献者可以帮助我们。

【讨论】:

    猜你喜欢
    • 2013-05-27
    • 2018-12-10
    • 2020-03-07
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    相关资源
    最近更新 更多