如果用户未登录,我想将用户发送到注册#new页面。
输入登录信息并单击“提交”后,我希望您能够将其发送到注册#show页面。
当您未登录时,它会将您发送到注册#new页面(到目前为止正确)。但是当您提交登录表单时,它会通过重定向循环发送错误。服务器的输出就是这个块一遍又一遍地重复:
Started GET "/" for 127.0.0.1 at 2013-09-25 02:31:59 -0400
Processing by RegistrationsController#new as HTML
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 8 ORDER BY "users"."id" ASC LIMIT 1
Redirected to http://lvh.me:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.7ms)
我似乎无法弄明白。它会让您登录,因为我可以看到通过手动导航到其他页面,但authenticated
根路径无法正常工作。我在我的路线文件中尝试了几种不同的组合,似乎无法得到它。我正在使用的代码基于this thread
在我的应用程序控制器中,我有before_filter :authenticate_user!
我的路线档案:
devise_for :users, :controllers => {
:registrations => "registrations"
}
devise_scope :user do
root to: "registrations#new"
end
authenticated :user do
root to: "registrations#show", :as => "profile"
end
unauthenticated do
root to: "registrations#new", :as => "unauthenticated"
end
答案 0 :(得分:24)
首先,您应该自定义Devise::RegistrationsController
(您可以添加文件app/controllers/registrations_controller.rb
)
在设计registrations_controller.rb
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
将show
操作添加到prepend_before_filter :authenticate_scope!
<强> registrations_controller.rb 强>
class RegistrationsController < Devise::RegistrationsController
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy, :show]
# GET /resource/sign_up
def new
super
end
# POST /resource
def create
super
end
# GET /resource/edit
def edit
super
end
def update
super
end
# DELETE /resource
def destroy
super
end
def show
end
protected
def after_sign_up_path_for(resource)
after_sign_in_path_for(resource)
end
end
同时将devise registration(修改新模板)的视图复制到/app/views/registrations/
,然后您可以在show.html.erb
文件夹中为个人资料页面制作文件/app/views/registrations/
。
对于设计路线看起来像:
devise_for :users, :skip => [:registrations]
devise_for :users, :controllers => {
:registrations => "registrations"
}
authenticated :user do
devise_scope :user do
root to: "registrations#show", :as => "profile"
end
end
unauthenticated do
devise_scope :user do
root to: "registrations#new", :as => "unauthenticated"
end
end
最后,您要在after_sign_in_path_for(resource)
和after_sign_out_path_for(resource_or_scope)
的文件 application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
private
def after_sign_in_path_for(resource)
# After you enter login info and click submit, I want you to be sent to the registrations#show page
profile_path
end
def after_sign_out_path_for(resource_or_scope)
new_user_session_path
end
end
答案 1 :(得分:8)
重定向到registration#new
是默认的,所以你需要做的就是这个(在你的路线文件中):
devise_for :users, :controllers => {
:registrations => "registrations"
}
devise_scope :user do
root to: "registrations#show" # This is the root path of the user when you are logged in
end
unauthenticated do
root to: "registrations#new", :as => "unauthenticated"
end
# I dont't think this part is neccesary... Try if it works without
authenticated :user do
root to: "registrations#show", :as => "profile"
end
答案 2 :(得分:2)
请勿使用路线执行属于控制器的作业。
要在注册后将用户重定向到特定页面,请使用Devise内置after_sign_up_path_for
并覆盖它。
class ApplicationController
def after_sign_up_path_for(resource)
faked_user_profile_path(resource)
end
end
对于路线,除了devise_for
部分之外,我并不是很了解所有路线。但是对于重定向功能,这个重写应该足够了。