问题描述
如果用户没有登录,我想将他们发送到registrations#new 页面.
I want to send a user to the registrations#new page if they aren't logged in.
在您输入登录信息并单击提交后,我希望您被发送到注册#show 页面.
After you enter login info and click submit, I want you to be sent to the registrations#show page.
当您未登录时,它会将您转到registrations#new 页面(目前为止正确).但是当您提交登录表单时,它会通过重定向循环发送错误.服务器的输出就是这个块一遍又一遍地重复:
It sends you to the registrations#new page when you're not logged in (correct so far). But when you submit the login form, it sends errors out with a redirect loop. The server's output is just this block repeated over and over:
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
I can't seem to figure it out. It does log you in, as I can see that by manually navigating to a different page, but the authenticated
root path is not working correctly. I've tried a few different combinations in my routes file and can't seem to get it. The code I'm using is based off of this thread
在我的应用程序控制器中,我有 before_filter :authenticate_user!
In my application controller I have 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
推荐答案
首先要自定义Devise::RegistrationsController
(可以添加文件app/controllers/registrations_controller.rb
代码>)
First, you should customize Devise::RegistrationsController
(you can add file app/controllers/registrations_controller.rb
)
并查看 prepend_before_filter 在设计 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
同时复制设计注册的视图(编辑和新模板)到 /app/views/registrations/
并且您可以在 /app/views/registrations/
文件夹中创建一个文件 show.html.erb
用于个人资料页面.
Also copy the view of devise registration (edit and new template) to /app/views/registrations/
and You can make a file show.html.erb
in /app/views/registrations/
folder for a profile page.
设计路线如下:
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
最后,您在 application_controller.rb 文件中的 after_sign_in_path_for(resource)
和 after_sign_out_path_for(resource_or_scope)
中设置路径"p>
Last, you to set a "path" in after_sign_in_path_for(resource)
and after_sign_out_path_for(resource_or_scope)
at file 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
注意:我已经尝试过这个(制作示例应用程序),它可以工作.登录时查看日志这里,退出登录这里一个>
Note: I have tried this (to make sample apps), and it works. See log when sign in here, and log sign out here
这篇关于使用 rails 4 认证的根路由进行设计无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!