设计与rails 4认证根路由不工作 [英] Devise with rails 4 authenticated root route not working

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

问题描述

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

I want to send a user to the registrations#new page if they aren't logged in.

输入登录信息后,单击提交,我希望您被发送到注册#显示页面。

After you enter login info and click submit, I want you to be sent to the registrations#show page.

当您未登录(到目前为止),它会将您发送到注册#新页面。但是,当您提交登录表单时,它会使用重定向循环发送错误。服务器的输出只是这个块反复重复:

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)

我似乎无法弄清楚。它确实会记录您,正如我可以看到,通过手动导航到不同的页面,但认证的根路径无法正常工作。我在路由文件中尝试了几种不同的组合,似乎没有得到它。我使用的代码是基于此主题

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)

请参阅 prepend_before_filter on devise registrations_controller.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 action to prepend_before_filter:authenticate_scope!

Add show action to prepend_before_filter :authenticate_scope!

registrations_controller.rb p>

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 /注册/ ,您可以在 / app / views / registrations / show.html.erb c $ c>文件夹为配置文件页。

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

最后,你要设置一个路径在$ code> after_sign_in_path_for(资源)和 after_sign_out_path_for(resource_or_scope)在文件 application_controller.rb / p>

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天全站免登陆