gpt4 book ai didi

ruby-on-rails - 使用 rails 4 认证的根路由进行设计无法正常工作

转载 作者:行者123 更新时间:2023-12-04 05:29:06 25 4
gpt4 key购买 nike

我正在尝试做的事情

如果他们没有登录,我想将用户发送到registrations#new 页面。

在您输入登录信息并单击提交后,我希望您被发送到registrations#show 页面。

怎么了

当您未登录时,它会将您发送到 registrations#new 页面(到目前为止正确)。但是当您提交登录表单时,它会通过重定向循环发送错误。服务器的输出就是这个 block 一遍又一遍地重复:

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

我的代码

在我的应用程序 Controller 中,我有 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 )

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]

添加 showprepend_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

注意:我已经尝试过(制作示例应用程序),它可以工作。登录时查看日志 here , 并注销 here

关于ruby-on-rails - 使用 rails 4 认证的根路由进行设计无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18997874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com