我正在使用 Devise 作为我的应用程序的身份验证机制。
我想向我的用户模型添加其他信息,例如用户统计信息、个人资料图片和其他有关用户的相关信息。如何实现?
即使在创建“rails g devise : views”之后,它也只创建我可以自定义的 View ,它不会给我它的 Controller 来编辑。
否则我可以编辑设计 gem 本身以满足我的要求吗?
我从未尝试过定制 GEM。是否有任何链接可以开始自定义现有的 gem?
最佳答案
Devise 本身是一个 Rails 引擎,您可以通过在本地目录中创建您希望更改的文件的副本来覆盖它的任何功能。当 Rails 开始为请求寻找合适的 Controller 时,它会首先检查本地应用程序,然后是 vendor/gems,然后是加载的 gem 本身。
在 Devise 的案例中,他们提到修改 Controller 应该以这种方式完成:
Configuring controllers
If the customization at the views level is not enough, you can customize each controller by following these steps:
1) Create your custom controller, for example a Admins::SessionsController:
class Admins::SessionsController < Devise::SessionsController end
2) Tell the router to use this controller:
devise_for :admins, :controllers => { :sessions => "admins/sessions" }
3) And since we changed the controller, it won’t use the "devise/sessions" views, so remember to copy "devise/sessions" to "admin/sessions".
Remember that Devise uses flash messages to let users know if sign in was successful or failed. Devise expects your application to call "flash[:notice]" and "flash[:alert]" as appropriate.
关于ruby-on-rails - 如何创建 Controller 和 View 以使用 Devise gem 作为身份验证机制来显示用户的个人资料?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4116834/