Ruby on rails 在rails 4认证根路径不工作的情况下设计 我想做什么
如果用户未登录,我想将其发送到注册新页面 输入登录信息并单击提交后,我希望您被发送到注册显示页面 发生了什么事 当您未登录时,它会将您发送到注册新页面(目前为止正确)。但是,当您提交登录表单时,它会通过重定向循环发送错误。服务器的输出就是反复重复的这个块:Ruby on rails 在rails 4认证根路径不工作的情况下设计 我想做什么,ruby-on-rails,devise,routes,ruby-on-rails-4,Ruby On Rails,Devise,Routes,Ruby On Rails 4,如果用户未登录,我想将其发送到注册新页面 输入登录信息并单击提交后,我希望您被发送到注册显示页面 发生了什么事 当您未登录时,它会将您发送到注册新页面(目前为止正确)。但是,当您提交登录表单时,它会通过重定向循环发送错误。服务器的输出就是反复重复的这个块: Started GET "/" for 127.0.0.1 at 2013-09-25 02:31:59 -0400 Processing by RegistrationsController#new as HTML User Load
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
根路径工作不正常。我在我的routes文件中尝试了几种不同的组合,但似乎无法获得它。我使用的代码是基于
我的代码
在我的应用程序控制器中,我在过滤器之前有:验证用户代码>
我的路由文件:
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
不要使用路由来执行属于控制器的作业
要在注册后将用户重定向到特定页面,请在
注册后使用designe内置的\u path\u并覆盖它
class ApplicationController
def after_sign_up_path_for(resource)
faked_user_profile_path(resource)
end
end
对于路线,除了为
部分设计之外,我不太了解所有路线。但是对于重定向功能来说,这种覆盖应该足够了。重定向到注册#新建
是默认设置,因此您只需执行以下操作(在路由文件中):
首先,您应该定制
design::RegistrationsController
(您可以添加文件app/controllers/registrations\u controller.rb
)
请参见Desiveregistrations\u controller.rb
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
将show
操作添加到prepend\u之前的\u过滤器:验证\u范围代码>
注册\u 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
最后,您需要在文件application\u controller.rb的中为(资源)
在\u-in\u-path\u之后设置一个“路径”
并为(资源或范围)在\u-out\u-path\u之后设置一个“路径”
class ApplicationController
注意:我已经尝试过这个(制作示例应用程序),它很有效。请参阅登录时登录和注销时登录我将尝试此操作,但根据Deave文档,这是实现此操作的方法…TMP,我尚未阅读Deave wiki全文,也不知道这一部分,因此我上一次的陈述可能不准确,我将删除该部分。但是,要实现您请求的功能,方法重写应该足够简单。出于某种原因,我仍然得到重定向循环,我感觉在的注册后的路径不起作用,它仍然将其发送回rootnevermind,您完全正确,我还必须在的路径中为定义签名后的
,因为注册和登录都需要去某个地方。我将暂缓接受这一点,因为我仍然想知道通过authenticated:user do
Update更有效的方法:第二个最好的答案实际上是signed\u in\u root\u path
但仍然希望authenticated
root answercool我明天会试试这个。你在rails 4应用程序中试用过吗?做得很好。我所做的只是修改我的路由文件,使之与你的一样!已验证和/或未验证的设计范围。然后,我在经过身份验证的Desive_scope blockThanx中添加了其余的登录路由。这救了我一天。在筛选器之前使用prepend\u:不需要\u身份验证,仅:[:编辑,:更新]在筛选器之前使用prepend\u:身份验证\u范围!,除了:[:编辑,:更新]但这不使用我看到的已验证的
和未验证的
方法,抱歉忘记了未验证的
,但我认为当您使用根设计范围时,已验证的
不是必需的。但是当我有时间的时候,我会尝试测试更多:)。这个问题看起来和我的问题完全一样吗?不,你在设计范围
块中有root to:“registrations#new”
,这正是你出现问题的主要原因。
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_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
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