如何访问 Devise 控制器?
设备中的控制器是自动生成的吗?你如何访问它们?
我知道你的看法 rails 生成 devise_views
。
Are controllers in devise automatically generated? How do you access them?
I know for views you dorails generate devise_views
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设您想要查看这些控制器以便修改或覆盖它们,Devise 现在提供了一个简单的生成器,可以在您的应用程序中重新创建它们的控制器,以使这变得简单。根据文档(这将是最新的):
1) 使用需要范围的生成器创建自定义控制器:
console
如果您指定
users
作为范围,控制器将在app 中创建/控制器/用户/
。会话控制器将如下所示:2) 告诉路由器使用此控制器:
3) 将视图从
devise/sessions
复制到users /会话
。由于控制器已更改,因此它将不会使用位于devise/sessions
中的默认视图。4) 最后,更改或扩展所需的控制器操作。
您可以完全覆盖控制器操作:
或者您可以简单地向其添加新行为:
这对于在某些操作期间触发后台作业或记录事件非常有用。
请记住,Devise 使用闪现消息来让用户知道登录是否成功。 Devise 希望您的应用程序根据需要调用
flash[:notice]
和flash[:alert]
。不要打印整个闪存哈希,仅打印特定键。在某些情况下,Devise 会向 flash 哈希添加一个:timedout
键,该键不用于显示。如果您打算打印整个散列,请从散列中删除此键。Under the assumption that you want to see these controllers in order to modify or override them, Devise now provides a simple generator which recreates their controllers in your app to make this easy. As per the documentation (which will be most up-to-date):
1) Create your custom controllers using the generator which requires a scope:
console
If you specify
users
as the scope, controllers will be created inapp/controllers/users/
. And the sessions controller will look like this:2) Tell the router to use this controller:
3) Copy the views from
devise/sessions
tousers/sessions
. Since the controller was changed, it won't use the default views located indevise/sessions
.4) Finally, change or extend the desired controller actions.
You can completely override a controller action:
Or you can simply add new behaviour to it:
This is useful for triggering background jobs or logging events during certain actions.
Remember that Devise uses flash messages to let users know if sign in was successful or unsuccessful. Devise expects your application to call
flash[:notice]
andflash[:alert]
as appropriate. Do not print the entire flash hash, print only specific keys. In some circumstances, Devise adds a:timedout
key to the flash hash, which is not meant for display. Remove this key from the hash if you intend to print the entire hash.Devise 使用内部控制器,您可以在自己的代码中访问这些控制器并对其进行子类化。它们位于
Devise
模块下。例如,要扩展RegistrationsController
:那么您所要做的就是配置 Devise 的路由以使用您的控制器:
Devise uses internal controllers, which you can access and subclass in your own code. They are under the
Devise
module. For example, to extend theRegistrationsController
:Then all you have to do is configure Devise's routes to use your controller instead:
$railsgenerate devise:controllers SCOPE [options]
选项:
-c, [--controllers=one 二三]
选择要生成的特定控制器(确认、密码、注册、会话、解锁、omniauth_callbacks)
使用 -c 指定要覆盖哪个控制器。
如果不指定控制器,则将创建所有设备控制器。
例如:
rails generated devise:controllers users -c=sessions
这将在 app/controllers/users/sessions_controller.rb 中创建一个控制器类,如下所示:
$ rails generate devise:controllers SCOPE [options]
Options:
-c, [--controllers=one two three]
Select specific controllers to generate (confirmations, passwords, registrations, sessions, unlocks, omniauth_callbacks)
Use -c to specify which controller you want to overwrite.
If you do not specify a controller, all devise controllers will be created.
For example:
rails generate devise:controllers users -c=sessions
This will create a controller class at app/controllers/users/sessions_controller.rb like this:
下面是针对Rails 5的
使用以下命令生成rails devise控制器:
如果您修改了上面生成的控制器,请将以下行添加到
routes.rb
中,您的修改将重启rails服务器后生效
Below one is for Rails 5
Generate rails devise controllers using the following command:
if you modified the above generated controllers, add the following line to the
routes.rb
,Your modifications will take effect once you restart the rails server
要定义自定义控制器行为,
请参阅@ErikTrautman 的答案。
但是,如果您想了解 Devise 在幕后所做的事情,
则必须检查源代码(具体来说,在项目根目录的
app/
目录中)。 @MatheusMoreira 提供了 GitHub 上源代码的链接,但如果您想在自己的文本编辑器中本地浏览它,则可以使用gem which devise
找到 Devise gem 的安装位置。例如,要查看
Devise::SessionsController
:(或者,您可以克隆 git 存储库并以这种方式浏览。)
To define custom controller behavior,
see @ErikTrautman's answer.
But if you're trying to understand what Devise is doing under the hood,
you must inspect the source (specifically, in the project root's
app/
directory). @MatheusMoreira provides a link to the source on GitHub, but if you'd rather browse it locally in your own text editor, you can find the install location of the Devise gem withgem which devise
.For instance, to see
Devise::SessionsController
:(Or, you could just clone the git repo and poke around that way.)