如何访问 Devise 控制器?

发布于 2024-11-14 05:12:28 字数 82 浏览 2 评论 0原文

设备中的控制器是自动生成的吗?你如何访问它们?

我知道你的看法 rails 生成 devise_views

Are controllers in devise automatically generated? How do you access them?

I know for views you do
rails generate devise_views.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半葬歌 2024-11-21 05:12:28

假设您想要查看这些控制器以便修改或覆盖它们,Devise 现在提供了一个简单的生成器,可以在您的应用程序中重新创建它们的控制器,以使这变得简单。根据文档(这将是最新的):

1) 使用需要范围的生成器创建自定义控制器:

console

rails generate devise:controllers [scope]

如果您指定 users 作为范围,控制器将在 app 中创建/控制器/用户/。会话控制器将如下所示:

class Users::SessionsController < Devise::SessionsController
  # GET /resource/sign_in
  # def new
  #   super
  # end
  ...
end

2) 告诉路由器使用此控制器:

devise_for :users, controllers: { sessions: "users/sessions" }

3) 将视图从 devise/sessions 复制到 users /会话。由于控制器已更改,因此它将不会使用位于 devise/sessions 中的默认视图。


4) 最后,更改或扩展所需的控制器操作。

您可以完全覆盖控制器操作:

class Users::SessionsController < Devise::SessionsController
  def create
    # custom sign-in code
  end
end

或者您可以简单地向其添加新行为:

class Users::SessionsController < Devise::SessionsController
  def create
    super do |resource|
      BackgroundWorker.trigger(resource)
    end
  end
end

这对于在某些操作期间触发后台作业或记录事件非常有用。

请记住,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

rails generate devise:controllers [scope]

If you specify users as the scope, controllers will be created in app/controllers/users/. And the sessions controller will look like this:

class Users::SessionsController < Devise::SessionsController
  # GET /resource/sign_in
  # def new
  #   super
  # end
  ...
end

2) Tell the router to use this controller:

devise_for :users, controllers: { sessions: "users/sessions" }

3) Copy the views from devise/sessions to users/sessions. Since the controller was changed, it won't use the default views located in devise/sessions.


4) Finally, change or extend the desired controller actions.

You can completely override a controller action:

class Users::SessionsController < Devise::SessionsController
  def create
    # custom sign-in code
  end
end

Or you can simply add new behaviour to it:

class Users::SessionsController < Devise::SessionsController
  def create
    super do |resource|
      BackgroundWorker.trigger(resource)
    end
  end
end

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] and flash[: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.

格子衫的從容 2024-11-21 05:12:28

Devise 使用内部控制器,您可以在自己的代码中访问这些控制器并对其进行子类化。它们位于 Devise 模块下。例如,要扩展 RegistrationsController

class MembershipsController < Devise::RegistrationsController
  # ...
end

那么您所要做的就是配置 Devise 的路由以使用您的控制器:

devise_for :members, :controllers => { :registrations => 'memberships' }

Devise uses internal controllers, which you can access and subclass in your own code. They are under the Devise module. For example, to extend the RegistrationsController:

class MembershipsController < Devise::RegistrationsController
  # ...
end

Then all you have to do is configure Devise's routes to use your controller instead:

devise_for :members, :controllers => { :registrations => 'memberships' }
删除→记忆 2024-11-21 05:12:28

$railsgenerate devise:controllers SCOPE [options]

选项:
-c, [--controllers=one 二三]

选择要生成的特定控制器(确认、密码、注册、会话、解锁、omniauth_callbacks)

使用 -c 指定要覆盖哪个控制器。
如果不指定控制器,则将创建所有设备控制器。
例如:

rails generated devise:controllers users -c=sessions

这将在 app/controllers/users/sessions_controller.rb 中创建一个控制器类,如下所示:

 class Users::ConfirmationsController < Devise::ConfirmationsController
    content...
 end

$ 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:

 class Users::ConfirmationsController < Devise::ConfirmationsController
    content...
 end
往事随风而去 2024-11-21 05:12:28

下面是针对Rails 5的

使用以下命令生成rails devise控制器:

rails generate devise:controllers users

如果您修改了上面生成的控制器,请将以下行添加到routes.rb中,

devise_for :users, controllers: {registrations:'user/registrations'}

您的修改将重启rails服务器后生效

Below one is for Rails 5

Generate rails devise controllers using the following command:

rails generate devise:controllers users

if you modified the above generated controllers, add the following line to the routes.rb,

devise_for :users, controllers: {registrations:'user/registrations'}

Your modifications will take effect once you restart the rails server

苏大泽ㄣ 2024-11-21 05:12:28

要定义自定义控制器行为,

请参阅@ErikTrautman 的答案。

但是,如果您想了解 Devise 在幕后所做的事情,

则必须检查源代码(具体来说,在项目根目录的 app/ 目录中)。 @MatheusMoreira 提供了 GitHub 上源代码的链接,但如果您想在自己的文本编辑器中本地浏览它,则可以使用 gem which devise 找到 Devise gem 的安装位置。

例如,要查看 Devise::SessionsController:(

$ vim $(gem which devise | sed 's|\(.*\)\(/.*\)\{2\}|\1|')/app/controllers/devise/sessions_controller.rb

或者,您可以克隆 git 存储库并以这种方式浏览。)

$ git clone https://github.com/plataformatec/devise
$ cd devise
$ vim app/controllers/devise/sessions_controller.rb

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 with gem which devise.

For instance, to see Devise::SessionsController:

$ vim $(gem which devise | sed 's|\(.*\)\(/.*\)\{2\}|\1|')/app/controllers/devise/sessions_controller.rb

(Or, you could just clone the git repo and poke around that way.)

$ git clone https://github.com/plataformatec/devise
$ cd devise
$ vim app/controllers/devise/sessions_controller.rb
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文