【问题标题】:How to render a file from Devise module?如何从设计模块渲染文件?
【发布时间】:2015-09-02 10:50:57
【问题描述】:

我在我的 rails 应用程序中使用 devise gem 进行身份验证。在 idm_authenticable.rb 我有一个方法如下..

 module Devise
  module Strategies
   class IdmAuthenticatable < Authenticatable
     def create_employee
       if !employee.save
         render 'devise/sessions/new'
       end
     end
   end
  end
 end

但是上面的方法是抛出

 undefined method `render' for #<Devise::Strategies::IdmAuthenticatable:0x00000001771068>

我认为我们不能从模块内部进行渲染。有没有其他方法可以在设计模块中呈现错误页面?

【问题讨论】:

  • 是什么让您认为render 是在您的班级中定义的?
  • @Nathan 我没听明白。
  • render 定义在哪里?
  • 渲染未定义。我只是像我们在控制器中所做的那样使用。

标签: ruby-on-rails ruby ruby-on-rails-3 devise


【解决方案1】:

你不能像那样随意打电话给renderrender 方法在 ActionController::Rendering 中定义,它被混入到 Rails 中的控制器和视图堆栈中。

http://apidock.com/rails/ActionController/Rendering/render

您可以尝试将该模块包含到您的课程中,但我不建议您这样做,因为这听起来您并不完全确定自己在做什么。

class IdmAuthenticatable < Authenticatable
  include ActionController::Rendering
end

【讨论】:

  • 现在我得到未定义的局部变量或方法`response_body
  • 就像我说的,我不会推荐它。它是 Rails 中更大的混合模块堆栈的一部分,除非你非常了解这些模块,否则你不会让它按照你想要的方式工作。我的回答主要是你不能做你想做的事。您应该发布一个新问题来描述您要解决的问题......因为您当前使用的实施方法不合理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 2016-11-13
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
相关资源
最近更新 更多