Ric*_*kes 5 ruby-on-rails devise
我有一个旧版 Rails 3.2 应用程序,其中使用 Devise 进行身份验证。以前,Devise 一直使用其 SessionController 的默认应用程序布局,并且所有应用程序的控制器均显式声明layout "public"。我决定将公共布局更改为application.haml,并将之前的应用程序布局更改为devise.haml. 为了确保Devise::SessionsController使用正确的布局,我将其添加到我的application.rb文件中:
config.to_prepare do
Devise::SessionsController.layout "devise"
end
Run Code Online (Sandbox Code Playgroud)
但是,当我添加此内容时,我的集成测试开始失败,因为它们无法访问 ApplicationHelper 中定义的视图助手。为什么以这种方式更改控制器的布局会阻止使用 ApplicationHelper 方法?我该如何解决这个问题?
根据此响应,我通过在有问题的控制器内部声明来包含在 application_helpers.rb 中找到的所有帮助程序helper "manager/application"(如果“manager”是安装设备的可安装引擎的当前命名空间。如果您从以下位置调用它,则只需使用“application”标准应用程序)。
SessionsController 的示例
require_dependency "manager/application_controller"
module Manager
class SessionsController < Devise::SessionsController
# Without this line I get errors like 'undefined method link_to'
helper "manager/application"
layout "manager/application"
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
sign_in_and_redirect(resource_name, resource)
end
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
return render :json => {:success => true}
end
def failure
return render :json => {:success => false, :errors => ["Login failed."]}
end
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1076 次 |
| 最近记录: |