Ruby-on-rails 您如何访问设计控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6234045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:19:04  来源:igfitidea点击:

How do you access Devise controllers?

ruby-on-railsdevise

提问by katie

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

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

I know for views you do rails generate devise_views.

我知道你的看法 rails generate devise_views

采纳答案by Matheus Moreira

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

Devise 使用内部控制器,您可以在自己的代码中访问和子类化。它们在Devise模块下。例如,要扩展RegistrationsController

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

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

然后你所要做的就是配置 Devise 的路由来使用你的控制器:

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

回答by Erik Trautman

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

假设您希望看到这些控制器以修改或覆盖它们,Devise 现在提供了一个简单的生成器,它可以在您的应用程序中重新创建它们的控制器以简化此操作。根据文档(将是最新的):

1) Create your custom controllers using the generator which requires a scope:

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

console

安慰

rails generate devise:controllers [scope]

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

如果您指定users范围,控制器将在app/controllers/users/. 会话控制器将如下所示:

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


2) Tell the router to use this controller:

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

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


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

3) 将视图从 复制devise/sessionsusers/sessions。由于控制器已更改,因此它不会使用位于devise/sessions.



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

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

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 :timedoutkey 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 使用 Flash 消息让用户知道登录是成功还是失败。设计预计您的应用程序调用flash[:notice]flash[:alert]适当。不要打印整个闪存哈希,只打印特定的键。在某些情况下,Devise 会:timedout在 flash 哈希中添加一个键,该键不用于显示。如果您打算打印整个散列,请从散列中删除此键。

回答by Nimish

$ rails generate devise:controllers SCOPE [options]

$ rails generate devise:controllers SCOPE [options]

Options: -c, [--controllers=one two three]

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

Select specific controllers to generate (confirmations, passwords, registrations, sessions, unlocks, omniauth_callbacks)

选择要生成的特定控制器(确认、密码、注册、会话、解锁、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:

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

rails generate devise:controllers users -c=sessions

rails generate devise:controllers users -c=sessions

This will create a controller class at app/controllers/users/sessions_controller.rb like this:

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

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

回答by Naveen Kumar Madipally

Below one is for Rails 5

下面一个是 Rails 5

Generate rails devise controllers using the following command:

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

rails generate devise:controllers users

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

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

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

Your modifications will take effect once you restart the rails server

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

回答by Ryan Lue

To define custom controller behavior,

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

see @ErikTrautman's answer.

请参阅@ErikTrautman 的回答。

But if you're trying to understand what Devise is doing under the hood,

但是如果你想了解 Devise 在幕后做了什么,

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.

您必须检查源代码(特别是在项目根app/目录中)。@MatheusMoreira 提供了 GitHub 上源代码的链接,但如果您更愿意在自己的文本编辑器中本地浏览它,您可以使用gem which devise.

For instance, to see Devise::SessionsController:

例如,要查看Devise::SessionsController

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

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

(或者,您可以克隆 git repo 并以这种方式进行查看。)

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