我真的在这里错过了一些东西。
我已经阅读了很多关于设计重定向的内容(对于大多数人来说似乎很难实现......)但就我而言,我真的不明白。
有时我读到方法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
我正在使用带有 Ruby 1.9.3 和 Rails 3.2.13 的 Devise 3.1.0
谢谢您的帮助!
编辑
感谢@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
任何想法?