设计after_sign_in_path_for作品,而不是其他作品 [英] Devise after_sign_in_path_for works, but not the other ones

查看:389
本文介绍了设计after_sign_in_path_for作品,而不是其他作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经阅读了很多关于设计重定向的内容(好像很难为大多数人实现...)但是在我的情况下,我真的没有得到它。



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



我宁愿把它们放在我的 ApplicationController 中,因为它打扰了我创建更多的控制器只是一个重定向,但如果不可能在最后,我赢了不要坚持...



这是交易:



我在我的 ApplicationController (和其他一些,但这足够的例子)

  def after_update_path_for(user) 
flash [:notice] ='成功更新密码'
edit_user_path(用户)
end

def after _inactive_sign_up_path_for(用户)
flash [:notice] ='欢迎!请按照步骤!'
me_new_path
end

def after_sign_up_path_for(用户)
flash [:notice] ='欢迎!请按照步骤!'
me_new_path
end

def after_sign_in_path_for(用户)
如果user.sign_in_count == 1
me_new_path
其他
root_path
end
end

而且疯狂的事情是调用 after_sign_in_path_for ,而不是其他。就像用户注册一样,如果user.sign_in_count == 1 重定向他,而不是 after_inactive_sign_up_path_for 而不是 after_sign_up_path_for



如何来?



它可能与我的路线有关,所以这里是我的 routes.rb extract:

  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路线的结果

  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)编辑/密码#new
edit_user_password GET /password/edit(.:format)devise / passwords#edit
PUT /password(.:format)devise / passwords#update
cancel_user_registration GET / register /cancel(.:format)设计/注册#取消
user_registration POST /register(.:format)设计/注册#创建
new_user_registration GET /register(.:format)设计/注册#new
edit_user_registration GET /register/edit(.:format)设计/注册#edit
PUT /register(.:format)设计/注册#update
DELETE /register(.:format)devise / registrations#destroy
user_confirmation POST /confirmation(.:format)devise / confirmations#create
new_user_confirmation GET / confirmation / new(.:格式)设计/确认#新
GET /confirmation(.:format)设计/确认#显示

任何想法?

解决方案

所以感谢@ rich-peck的帮助,我们弄清楚了。 b
$ b

问题是为什么 after_sign_in_path_for 的行为与 after_sign_up_path_for 和cie? / p>

它出现,在设计来源中, after_sign_in_path_for 在帮助器中定义,而其他是他们的控制器的方法(例如 Devise :: RegistrationsController<因此,对于 after_sign_in_path_for ,它在 ApplicationController ,而对于其他的,需要创建一个 registrations_controller.rb 文件,以覆盖其中的方法:

  class RegistrationsController< Devise :: RegistrationsController 
protected

def after_sign_up_path_for(copasser)
flash [:notice] ='欢迎!请按照步骤!
me_new_path
end

end

并以这种方式设置 router.rb

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

我认为行为是不同的,因为可注册的,可恢复的等是设计的模块,不一定使用,在这种情况下帮助者不合适。一个设计者可以帮助我们。


I'm really missing something here.

I have read many things about devise redirects (seems like it is hard to implement for most people ...) but in my case I really don't get it.

Sometimes I read that the methods after_<action>_path_for(resource) should be in the ApplicationController, sometimes it is mentionned to be in a specific controller, overriding the devise one.

I'd rather have them in my ApplicationController since it bothers me to create more Controllers just for a redirect, but if it is not possible at the end, I won't insist...

Here's the deal:

I have in my ApplicationController : (and some others, but that's enough for the example)

  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

And the crazy thing, is that after_sign_in_path_for is called, but not the other ones. Like when the user signs up it's the if user.sign_in_count == 1 that redirects him, not the after_inactive_sign_up_path_for nor the after_sign_up_path_for

How come?

It could be related to my routes, so here's my routes.rb extract:

  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

And I'm using Devise 3.1.0 with Ruby 1.9.3 and Rails 3.2.13

Thanks for the help!


EDIT

Thanks @rich-peck for your answer. I updated my routes.rb this way :

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

which gives me the same routes as the previous ones (except I can't use login_path helper anymore but it's not a big deal), but I still get the same results concerning redirects.

Here is the result of 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

Any idea?

解决方案

So thanks to @rich-peck's help, we figured it out.

The question was why do after_sign_in_path_for behave differently than after_sign_up_path_for and cie ?

It appears, in the devise sources, that after_sign_in_path_for is defined in a helper, whereas the other ones are methods of their controller (eg. Devise::RegistrationsController < DeviseController)

Hence for after_sign_in_path_for it works overriding in the ApplicationController, whereas for the other ones, it is necessary to create a registrations_controller.rb file, to override the method in it:

class RegistrationsController < Devise::RegistrationsController
  protected

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

end

and to set the router.rb this way:

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

I suppose the behaviour is different because :registerable, :recoverable etc. are modules of devise, not necessarily used, and the helper wouldn't be appropriate in that case. A devise contributor could help us on this point.

这篇关于设计after_sign_in_path_for作品,而不是其他作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆