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

时间:2013-09-25 06:37:06

标签: ruby-on-rails devise routes ruby-on-rails-4

我正在尝试做什么

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

我的代码

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

3 个答案:

答案 0 :(得分:24)

首先,您应该自定义Devise::RegistrationsController(您可以添加文件app/controllers/registrations_controller.rb

在设计registrations_controller.rb

上查看prepend_before_filter
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

同时将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

答案 1 :(得分:8)

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

答案 2 :(得分:2)

请勿使用路线执行属于控制器的作业。

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

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

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