【问题标题】:Routing with Devise confirmable可确认设计的路由
【发布时间】:2012-10-05 09:37:42
【问题描述】:

我已经实现了 Devise Confirmable,它工作正常。当用户第一次注册时:

  • 重定向到 localhost:3000/en 的应用程序根目录

  • Flash 通知显示“带有确认链接的消息已发送到您的电子邮件地址。请打开链接以激活您的帐户。”

到目前为止一切顺利。

然后我更新了应用程序控制器:

class ApplicationController < ActionController::Base
    before_filter :authenticate_user!
end

然后,新注册的用户会发现自己被重定向到 localhost:3000/en/users/sign_in,并显示令人困惑的消息“您需要先登录或注册才能继续。”

我一直在浏览 Devise how to's,但还没有破解它。解决此问题的最简单方法是什么?

更新

这里是应用控制器的更多细节:

class ApplicationController < ActionController::Base
  protect_from_forgery                                  
  before_filter :authenticate_user!
  before_filter :set_i18n_locale_from_params

  rescue_from CanCan::AccessDenied do |exception|
    flash[:error] = "Access denied!"
    redirect_to root_url
  end

  protected

  def set_i18n_locale_from_params
        ...
  end
end

更新

这是所要求的日志。

Started POST "/en/users" for 127.0.0.1 at 2012-10-05 17:28:37 +0100
[17:28:37] Processing by Devise::RegistrationsController#create as HTML
[17:28:37]   Parameters: {"utf8"=>"✓", "authenticity_token"=>"xUGLBMvZVWrtqLWpnAsa7R361QaGrOz/aVfEwvlS6xY=", "user"=>{"email"=>"...@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up", "locale"=>"en"}
[17:28:37]    (0.1ms)  BEGIN
[17:28:37]   User Exists (0.6ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = '...@gmail.com' LIMIT 1
[17:28:37]   User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'a2XPMojypDn6uzzVAqcs' LIMIT 1
[17:28:37]   SQL (16.6ms)  INSERT INTO "users" ("confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "failed_attempts", "last_sign_in_at", "last_sign_in_ip", "locale", "locked_at", "remember_created_at", "reset_password_sent_at", "reset_password_token", "role", "sign_in_count", "unconfirmed_email", "unlock_token", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21) RETURNING "id"  [["confirmation_sent_at", Fri, 05 Oct 2012 16:28:38 UTC +00:00], ["confirmation_token", "a2XPMojypDn6uzzVAqcs"], ["confirmed_at", nil], ["created_at", Fri, 05 Oct 2012 16:28:38 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "...@gmail.com"], ["encrypted_password", "$2a$10$HnsOCZgVTaEg9bniVzBe7OKIvw4x5hdAqjwrCG14eQDCvSTz8mxdu"], ["failed_attempts", 0], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["locale", nil], ["locked_at", nil], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["role", 0], ["sign_in_count", 0], ["unconfirmed_email", nil], ["unlock_token", nil], ["updated_at", Fri, 05 Oct 2012 16:28:38 UTC +00:00]]
[17:28:37]   Rendered devise/mailer/confirmation_instructions.html.erb (0.8ms)
[17:28:37] 
Sent mail to ...
[17:28:37]    (0.8ms)  COMMIT
[17:28:37] Redirected to http://localhost:3000/en
[17:28:37] Completed 302 Found in 5050ms (ActiveRecord: 0.0ms)
[17:28:42] 

Started GET "/en" for 127.0.0.1 at 2012-10-05 17:28:42 +0100
[17:28:42] Processing by PageController#index as HTML
[17:28:42]   Parameters: {"locale"=>"en"}
[17:28:42] Completed 401 Unauthorized in 0ms
[17:28:42] 

Started GET "/users/sign_in" for 127.0.0.1 at 2012-10-05 17:28:42 +0100
[17:28:42] Processing by Devise::SessionsController#new as HTML
[17:28:42]   Rendered devise/shared/_links.erb (0.9ms)
[17:28:42]   Rendered devise/sessions/new.html.erb within layouts/application (6.8ms)
[17:28:42] Completed 200 OK in 22ms (Views: 20.0ms | ActiveRecord: 0.0ms)

在 routes.rb 我有:

root :to => "page#index"

所以发生的事情是 PageController#index 正在重定向用户登录。所以问题归结为如何仅从 :authenticate_user 中排除这个!在应用程序控制器中。

分辨率

感谢jibiel 和this question,答案是:

  class PageController < ApplicationController
    skip_before_filter :authenticate_user!
  end

简单!

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:
    before_filter :authenticate_user!, :unless => :devise_controller?
    

    并且devise will handle 自己对其控制器操作进行身份验证。否则,您将覆盖它们,因为它们 预先添加 到您的 before_filter

    【讨论】:

    • 谢谢。恐怕这行不通。它的行为方式完全相同。我尝试重新启动服务器并将线路移动到应用程序控制器的最顶部。如果这有帮助,我会在问题中提供更多详细信息。
    • 那么请提供倒数第二个动作的日志。我认为它类似于 Started POST "/users" ...由 RegistrationsController#create 作为 HTML 处理。需要知道身份验证失败的确切位置。只有重定向困扰您吗?用户是否在案例中正确创建?
    • 这很有帮助。我已经发布了日志,其中显示了要免除的控制器操作:authenticate_user!
    • 你把我带到了那里。我已经发布了解决方案。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多