繁体   English   中英

用rails 4设计认证的根路由不起作用

[英]Devise with rails 4 authenticated root route not working

我想做什么

我想将用户发送到注册#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根路径无法正常工作。 我在我的路线文件中尝试了几种不同的组合,似乎无法得到它。 我正在使用的代码基于这个线程

我的代码

在我的应用程序控制器中,我有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]

show action添加到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

对于设计路线看起来像:

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)中设置“路径”

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

注意:我已经尝试了这个(制作示例应用程序),并且它有效。 查看日志的时候签在这里 ,然后登录登出这里

重定向到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

不要使用路由来执行属于控制器的作业。

要在注册后将用户重定向到特定页面,请使用Devise内置的after_sign_up_path_for并覆盖它。

class ApplicationController
  def after_sign_up_path_for(resource)
    faked_user_profile_path(resource)
  end
end

对于路由,除了devise_for部分之外,我并不是很了解所有这些devise_for 但是对于重定向功能,这个重写应该足够了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM