19

我正在尝试做的事情

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

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

怎么了

当您未登录时,它会将您发送到 registrations#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
4

3 回答 3

26

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

并在设计上查看prepend_before_filterregistrations_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!

注册控制器.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/您可以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

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

于 2013-10-08T08:15:22.650 回答
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
于 2013-10-07T11:29:07.467 回答
2

不要使用路由来完成属于控制器的工作。

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

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

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

于 2013-09-25T07:38:12.727 回答