使用 rails 4 身份验证的根路由设计不起作用 [英] Devise with rails 4 authenticated root route not working

查看:26
本文介绍了使用 rails 4 身份验证的根路由设计不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户未登录,我想将其发送到注册#新页面.

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)

在设计 registrations_controller 上查看 prepend_before_filter.rb

And see prepend_before_filter on devise 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.

对于设计路线看起来像:

For routes of devise looks like :

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

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 身份验证的根路由设计不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆